Last Modified: KB » [if] Shortcode Pro
Shortcode Attributes

The [if /] Shortcode can be used in many different ways. Below you will find documentation for all of the Shortcode Attributes that are currently supported, along with a few examples of their use. More examples can be found throughout our knowledgebase.

Shortcode Attributes

Shortcode Modifier Attributes

Other Special Features

Shortcode Attribute Documentation

php="{raw}"

Example 1

The php="{raw}" Attribute allows you to check a raw PHP expression. This Attribute is disabled by default, but you can easily enable it from the plugin options page in WordPress, thereby unlocking additional power & flexibility.

[if php="get_template() == 'christmas'"]
    Example content here.
    [else]
        Default content.
[/if]

Example 2

The php="{raw}" Attribute allows for very complex conditionals to be written by developers in raw PHP code.

[if php="(!empty($_REQUEST['promo']) && ($_REQUEST['promo'] == 'christmas' || $_REQUEST['promo'] == 'xmas')) || in_array(get_template(), ['christmas', 'xmas'])"]
    Example content here.
    [else]
        Default content.
[/if]


current_user_is_logged_in="{bool}"

Example 1

The current_user_is_logged_in="{bool}" Attribute allows you to test if the current User is logged-in, or not.

[if current_user_is_logged_in="true"]
    You are logged in.
    [else]
        Not logged in.
[/if]

Example 2

[if current_user_is_logged_in="false"]
    You are not logged in.
    [else]
        You are logged in.
[/if]


current_user_can="{capability|expr}"

Example 1

The current_user_can="{capability|expr}" Attribute allows you to test the current User's Capabilities in WordPress, via the current_user_can() function in the WordPress core.

Note: The Capability used in this example (access_pkg_elite) can be configured by a plugin like WooCommerce Restrictions. What's important to remember is that the name of any given Capability should be determined ahead of time (by you), so you'll know exactly what to check for when you write a conditional expression like the one you see below.

[if current_user_can="access_pkg_elite"]
    You can access the Elite Restriction package on a site running WooCommerce Restrictions.
    [else]
        Access denied.
[/if]

Example 2

The current_user_can="{capability|expr}" Attribute also supports Simple Expressions. You can use this Attribute to check multiple Capabilities using logical operators such as AND, OR. In the following example, the OR operator is used to check if the current User can access the Elite package, OR has the ability to edit_posts.

[if current_user_can="access_pkg_elite OR edit_posts"]
    You can access the Elite Restriction package on a site running WooCommerce Restrictions, or you can edit Posts in WordPress.
    [else]
        Access denied.
[/if]

Example 3

You can also group conditions using round brackets, much like you would in a raw PHP expression.

[if current_user_can="(access_pkg_elite AND access_ccap_extras) OR edit_posts"]
    You can access the Elite Restriction package w/ extras on a site running WooCommerce Restrictions, or you can edit Posts in WordPress.
    [else]
        Access denied.
[/if]


current_user_is_paying_customer="{bool}"

Example 1

The current_user_is_paying_customer="{bool}" Attribute allows you to test if the current User is a paying Customer, or not. This particular Attribute depends on you having the WooCommerce plugin for WordPress.

[if current_user_is_paying_customer="true"]
    You are a paying Customer.
    [else]
        Please buy something.
[/if]

Example 2

[if current_user_is_paying_customer="false"]
    Please buy something.
    [else]
        You are a paying Customer.
[/if]


current_user_bought_product="{ID|SKU|expr}"

Example 1

The current_user_bought_product="{ID|SKU|expr}" Attribute allows you to check (in WooCommerce) if the current User is a Customer who has bought a Product from you, or not. Please see notes below.

Note: This Attribute requires the WooCommerce plugin for WordPress. You can check a WooCommerce Product by its numeric ID, or by its SKU as defined in the Product Data section in WooCommerce.

Variable Products: Checking for a Product by the parent Product SKU (or parent Product ID) returns true if any variation of that Product was purchased. That is the most typical use case and the easiest way to work with this Attribute. On the other hand, if the ID or SKU that you check is variation-specific, this only returns true if that specific variation was purchased. In other words, if you want to check a specific variation, you'll need to do just that; i.e., check a specific variation ID or SKU.

[if current_user_bought_product="ACME-INSTALLER-PRO"]
    You bought something on a site running WooCommerce.
    [else]
        Please purchase.
[/if]

Example 2

The current_user_bought_product="{ID|SKU|expr}" Attribute also supports Simple Expressions. You can use this Attribute to check multiple Product IDs or SKUs using logical operators such as AND, OR. You can also group conditions using round brackets, much like you would in a raw PHP expression.

[if current_user_bought_product="(123 AND ACME-INSTALLER-PRO) OR ACME-ELITE-BUNDLE"]
    You bought something on a site running WooCommerce.
    [else]
        Please purchase.
[/if]


current_user_can_download="{ID|SKU|expr}"

Example 1

The current_user_can_download="{ID|SKU|expr}" Attribute allows you to check (in WooCommerce) if the current User is a Customer who can currently download a Product, or not. Please see notes below.

Important: It's worth noting that in WooCommerce, access to file downloads can be set to expire in a number of ways. That's why this particular Attribute is helpful. In short, this conditional check is not quite the same as simply using current_user_bought_product="{ID|SKU|expr}" Instead, this Attribute checks to see if they have bought the Product, and, that they can also still download at least one file associated with the Product that they purchased.

Note: This Attribute requires the WooCommerce plugin for WordPress. You can check a WooCommerce Product by its numeric ID, or by its SKU as defined in the Product Data section in WooCommerce.

Variable Products: Checking for a Product by the parent Product SKU (or parent Product ID) returns true if any variation of that Product was purchased. That is the most typical use case and the easiest way to work with this Attribute. On the other hand, if the ID or SKU that you check is variation-specific, this only returns true if that specific variation was purchased. In other words, if you want to check a specific variation, you'll need to do just that; i.e., check a specific variation ID or SKU.

[if current_user_can_download="ACME-INSTALLER-PRO"]
    You can download something on a site running WooCommerce.
    [else]
        Please purchase.
[/if]

Example 2

The current_user_can_download="{ID|SKU|expr}" Attribute also supports Simple Expressions. You can use this Attribute to check multiple Product IDs or SKUs using logical operators such as AND, OR. You can also group conditions using round brackets, much like you would in a raw PHP expression.

[if current_user_can_download="(123 AND ACME-INSTALLER-PRO) OR ACME-ELITE-BUNDLE"]
    You can download something on a site running WooCommerce.
    [else]
        Please purchase.
[/if]


current_user_meta="{key|expr}"

Example 1

The current_user_meta="{key|expr}" Attribute allows you to check a User meta value in WordPress. Underlying this Attribute is the get_user_meta() function in WordPress core.

Note: The difference between current_user_meta="{key|expr}" and current_user_option="{key|expr}" is that 'option' values are User meta keys pulled from the 'current' Child Site in a WordPress Multisite Network, whereas current_user_meta="{key|expr}" is a global meta value that is available across all Child Blogs in a WordPress Multisite Network. In a standard WordPress installation, there is no difference between the two. In a standard WordPress installation, current_user_meta="{key|expr}" is suggested.

[if current_user_meta="_money_spent > 0"]
    You bought something on a site running WooCommerce.
    [else]
        No purchase yet.
[/if]

Example 2

The current_user_meta="{key|expr}" Attribute also supports Simple Expressions. You can use this Attribute to check multiple meta values using logical operators such as AND, OR. You can also group conditions using round brackets, much like you would in a raw PHP expression.

[if current_user_meta="(paying_customer AND _order_count > 0) OR _money_spent > 0"]
    You bought something on a site running WooCommerce.
    [else]
        No purchase yet.
[/if]


current_user_option="{key|expr}"

Example 1

The current_user_option="{key|expr}" Attribute allows you to check any User meta value in WordPress. Underlying this Attribute is the get_user_option() function in WordPress core.

Note: The difference between current_user_option="{key|expr}" and current_user_meta="{key|expr}" is that 'option' values are User meta keys pulled from the 'current' Child Site in a WordPress Multisite Network, whereas current_user_meta="{key|expr}" is a global meta value that is available across all Child Blogs in a WordPress Multisite Network. In a Standard WordPress Installation, there is no difference between the two. In a standard WordPress installation, current_user_meta="{key|expr}" is suggested.

[if current_user_option="_money_spent > 0"]
    You bought something on a site running WooCommerce.
    [else]
        No purchase yet.
[/if]

Example 2

The current_user_option="{key|expr}" Attribute also supports Simple Expressions. You can use this Attribute to check multiple meta values using logical operators such as AND, OR. You can also group conditions using round brackets, much like you would in a raw PHP expression.

[if current_user_option="(paying_customer AND _order_count > 0) OR _money_spent > 0"]
    You bought something on a site running WooCommerce.
    [else]
        No purchase yet.
[/if]


request_var="{key|expr}"

Example 1

The request_var="{key|expr}" Attribute allows you to check GET|POST data (e.g., variables in a URL query string). Underlying this Attribute is the $_REQUEST array in PHP.

[if request_var="version == '1'"]
    You came to ?version=1 of this page.
[/if]
[if request_var="version == '2'"]
    You came to ?version=2 of this page.
[/if]
[if request_var="version != '1' AND version != '2'"]
    You came to the default version of this page.
[/if]

Example 2

The request_var="{key|expr}" Attribute also supports Simple Expressions. You can use this Attribute to check multiple request values using logical operators such as AND, OR. You can also group conditions using round brackets, much like you would in a raw PHP expression.

[if request_var="version == '1' AND (amount > 0 OR donation_amount > 0)"]
    You are ready to pay for something on my site.
        ~ e.g., ?version=1&amount=129.00
    [else]
        Please make a selection.
[/if]

Shortcode Modifier Attributes

_for_blog="{ID}"

The _for_blog="{ID}" Attribute works together with current_user_can="{capability|expr}", allowing you to check User permissions on a specific Child Blog in a WordPress Multisite Network. This Attribute is disabled by default, but you can easily enable it from the plugin options page in WordPress.

Note: The Capability used in this example (access_pkg_elite) can be configured by a plugin like WooCommerce Restrictions. What's important to remember is that the name of any given Capability should be determined ahead of time (by you), so you'll know exactly what to check for when you write a conditional expression like the one you see below.

Example

[if current_user_can="access_pkg_elite" _for_blog="8"]
    You can access the Elite Restriction package on Child Site #8, which is running the WooCommerce Restrictions plugin.
    [else]
        Access denied.
[/if]

See also: current_user_can="{capability|expr}" to learn more.


_satisfy="{any|all}"

Example 1

The _satisfy="{any|all}" Attribute defaults to a value of all, meaning that if you happen to use more than a single Attribute in the [if /] Shortcode, then it will only return true if 'all' conditions can be satisfied as true. That is the default behavior. However, you can change it to any if you prefer.

Note: The reason _satisfy="{any|all}" exists, is because you can use more than one Shortcode Attribute in each instance of the [if /] Shortcode. So while in most cases you'll find it easier to check one specific thing at a time, you can also have a single Shortcode that uses multiple Attributes when you need to consider more than one condition. This allows you to combine one Attribute with another. However, please remember that each Attribute name must still be unique; i.e., you can't check the exact same Attribute name twice.

[if current_user_can_download="ACME-INSTALLER-PRO" current_user_can="edit_posts" _satifsy="any"]
    You can download 'ACME-INSTALLER-PRO', or you're capable of editing Posts on this site.
    [else]
        Access denied.
[/if]

Example 2

Here, we don't need to use _satisfy="{any|all}", because we can use a Simple Expression instead. Note the use of OR in this example. A Simple Expression (i.e., AND, OR) is better/faster than _satisfy="{any|all}".

[if current_user_can_download="ACME-INSTALLER-PRO OR ACME-ELITE-BUNDLE"]
    You can download 'ACME-INSTALLER-PRO', or 'ACME-ELITE-BUNDLE'.
    [else]
        Access denied.
[/if]

Example 3 (Bad/Invalid)

Why bad? There are two identical Attributes, which makes the WordPress Shortcode invalid.

[if current_user_can_download="ACME-INSTALLER-PRO" current_user_can_download="ACME-ELITE-BUNDLE" _satifsy="any"]
    ... Don't do this. It's not a valid Shortcode.
[/if]


_debug="{bool}|verbose"

The _debug="{bool}|verbose" Attribute enables (or disables) a built-in debugger that comes with the [if /] Shortcode. The default value of this Attribute can be set from the plugin options page in WordPress. Therefore, if you set the default value for this Attribute in your plugin options, you probably won't need to use this on a per-Shortcode basis. Still, it's worth documenting the Attribute here in case you do, and also to help explain how the debugger works.

Example 1 (Bad/Invalid)

This example uses invalid syntax. The NOT operator (e.g., 123 NOT 456 as seen below) is a completely unsupported feature. Therefore, this is going to be a problem. If _debug="true", you will receive output (in red) when viewing a Post or Page in WordPress, or wherever the invalid Shortcode was used. The built-in debugger will let you know that you've made a mistake and suggest ways to correct the problem.

[if current_user_can_download="123 NOT 456" _debug="true"]
    ...This will trigger an error and the debugger will let you know about it.
[/if]

Example 2 (Bad/Invalid/Verbose)

This will also result in an error, and in this case (_debug="verbose"), you will get some additional output that may help you locate the underlying problem. It's worth noting that verbose output can often become confusing as well, so we suggest not using verbose unless you really are digging deep to find a problem.

[if current_user_can_download="123 NOT 456" _debug="verbose"]
    ...This will trigger an error and the debugger will let you know about it.
[/if]

Example 3 (Bad/Invalid; Fail Silently)

This will also result in an error, but in this case (_debug="false"), you will not get any debug info; i.e., it will fail silently. Note also that whenever you have invalid syntax, the [if /] Shortcode will refuse to return a true value, no matter what. Even if _debug="false", because the syntax is still invalid, regardless.

[if current_user_can_download="123 NOT 456" _debug="false"]
    ...This will trigger an error and fail silently.
[/if]