In addition to all of the Attributes that come with the [if /]
Shortcode, you can also convert any PHP function into a Shortcode Attribute automatically. This functionality is disabled by default, but you can easily enable it from the plugin options page in WordPress. This feature packs a lot of powerful functionality!
When Arbitrary Attributes are enabled, you can simply type the name of a PHP function as a Shortcode Attribute. This also means that when Arbitrary Attributes are enabled, many PHP functions that already come with WordPress core will instantly become valid Shortcode Attributes that are available to you. You can review a few examples below.
Compatible PHP Functions
There are two types of PHP functions that are compatible with the [if /]
Shortcode.
Conditional Utility Functions
You can use PHP functions that don't require any input parameters and return a boolean true or false value; e.g., is_front_page()
, is_page()
, comments_open()
. Simply use the name of the PHP function as a Shortcode Attribute and set it to a boolean value that you want to test for.
[if is_front_page="true"]...[/if]
[if comments_open="false"]...[/if]
Single-Parameter Functions
Functions capable of operating on a single input parameter; e.g., is_page(ID)
, is_singular(type)
, is_category(ID)
, has_tag(tag)
, get_user_option(key)
. Use the name of the PHP function as a Shortcode Attribute and set it to a parameter value that is passed to the underlying PHP function. Internally, the PHP function may return any scalar value. By default, the function's return value will be automatically converted to a boolean data type so you can easily test for a true or false condition, as seen in these examples.
[if is_category="541"]...[/if]
[if has_tag="members-only"]...[/if]
Simple Expressions are also supported for this type of PHP function, which means you can also check the return value of the underlying PHP function and compare it to something you'd like to test for explicitly.
[if get_user_option="key == 'value'"]...[/if]
[if get_user_option="show_admin_bar_front == 'true'"]...[/if]
In addition, Simple Expressions can be used to test multiple parameter values using logical operators such as AND
, OR
. You can even group conditionals using round brackets like you would in raw PHP.
[if is_page="1 OR 2"]...[/if]
[if in_category="123 OR (456 AND 789)"]...[/if]
[if get_user_option="key1 == 'value1' OR key2 == 'value2'"]...[/if]
More Arbitrary Attribute Examples
[if is_front_page="true"]
This is the front page.
[/if]
[if is_page="test"]
This is the `/test` Page.
[/if]
[if comments_open="true"]
Comments are open.
[/if]
[if is_page="prices OR landing"]
This is the `/prices` Page, or the `/landing` Page.
[/if]
[if is_page="!terms-conditions AND !privacy-policy"]
This is not the `/terms-conditions` Page, and it's not the `/privacy-policy` Page either.
[/if]
[if is_sticky="false"]
This is not sticky.
[/if]
[if is_super_admin="true"]
Current user is a Super Admin.
[/if]
[if is_woocommerce="true"]
The current page uses WooCommerce templates. Requires WooCommerce.
[/if]
[if is_shop="true"]
The current page is a product archive; i.e., WooCommerce shop. Requires WooCommerce.
[/if]
[if is_product="true"]
The current page is a single Product view. Requires WooCommerce.
[/if]
[if is_cart="true"]
The current page is the shopping cart. Requires WooCommerce.
[/if]
[if is_checkout="true"]
The current page is the shopping cart checkout page. Requires WooCommerce.
[/if]
[if is_account_page="true"]
The current page is the 'My Account' page. Requires WooCommerce.
[/if]
... and many others, including those provided by plugins that you activate.
See also: https://developer.wordpress.org/themes/basics/conditional-tags/
See also: https://docs.woocommerce.com/wc-apidocs/package-WooCommerce.Functions.html
Creating Custom Conditional Functions
Given the details above, you can see that Arbitrary Attributes also make it possible for you to create PHP functions yourself, and then use them together with the [if /]
Shortcode in WordPress. Just be sure that you review the details above regarding the two types of PHP functions that are compatible with the [if /]
Shortcode. Also, be sure to name your PHP functions using snake_case
so they can be used as a Shortcode Attribute.
Caveat: A potential hang-up with some functions for WordPress is that they require a $post_id
parameter instead of being able to automatically detect the current post ID. If you run into something like this, you can simply add a new function to your theme's functions.php
file. Here's a quick example.
<?php
function current_post_meta($key) {
$post_id = get_the_ID();
return get_post_meta($post_id, $key, true);
} // Referencing: <https://developer.wordpress.org/reference/functions/get_post_meta/>
Now we can use it like this as an [if /]
Shortcode Attribute.
[if is_page="true" current_post_meta="_wp_page_template !== ''"]
The current Page is using a custom Page template.
[/if]
Advanced Raw PHP Code
The [if /]
Shortcode also natively supports raw PHP code using the php="{raw}"
Attribute.