Last Modified: KB » [if] Shortcode Pro
Nesting Conditional Shortcodes

The idea is that you have a conditional check via the [if /] Shortcode. When the condition is true and content is displayed by that Shortcode, you might have another nested [if /] Shortcode within that particular block that checks something else. While this is absolutely possible, it requires an underscore _ prefix as outlined below.

Example 1 (Bad/Invalid)

Due to limitations in the WordPress Shortcode parser, the most obvious approach causes problems. This example doesn't work because the WordPress Shortcode parser doesn't understand Shortcodes of the same name, inside other Shortcodes of the same name. Therefore, this results in broken syntax. Why? The first closing [/if] tag is what WordPress will think is right, but it's not, because Shortcodes have actually been nested inside one another.

WARNING: The most obvious approach (shown here) does NOT work as expected!

[if current_user_is_logged_in="true"]

    [if current_user_can="access_something"]
        You can access something on this site.
        [else]
            Please make a purchase.
    [/if]

    [else]
        Please log in.
[/if]

Example 2 (Good/Alternative)

Instead, use a slight variation of the [if /] Shortcode. Notice here that all of the nested Shortcodes use an underscore _ prefix. In this way, there are actually two different Shortcodes for WordPress to parse, and so it is handled as expected in this case. Not as clean as we'd like, but it's the best available option at this time.

[if current_user_is_logged_in="true"]

    [_if current_user_can="access_something"]
        You can access something on this site.
        [_else]
            Please make a purchase.
    [/_if]
    
    [else]
        Please log in.
[/if]

Example 3 (Good; w/ Deeper Nesting)

The [if /] Shortcode supports nesting up to five levels deep. Therefore, [_____if /] and [_____else] are perfectly valid. If you find yourself needing conditionals that run deeper, please consider restructuring and/or combining some of your checks to help reduce the overall complexity.

[if current_user_is_logged_in="true"]

    [_if current_user_can="access_something"]
    
        You can access something on this site.
        
        [__if current_user_can="access_something_else"]
            You can access something else on this site.
            [__else]
                Please make a purchase.
        [/__if]
        
        [_else]
            Please make a purchase.
            
    [/_if]
    
    [else]
        Please log in.
[/if]

Tip: This last example could be rewritten in a way that avoids nested Shortcodes altogether.

[if current_user_is_logged_in="false"]
    Please log in.
[/if]
[if current_user_is_logged_in="true" current_user_can="access_something"]
    You are logged-in and you can access something on this site.
[/if]
[if current_user_is_logged_in="true" current_user_can="access_something AND access_something_else"]
    You are logged-in and you can access both of these Capabilities.
[/if]