Membership – Shortcodes

Simple Shortcode Examples w/ [s2If /]

There are two different Shortcodes being demonstrated here:

  1. [s2If /] (for testing simple conditional expressions).
  2. [s2Get /] (to get an API Constant value, a Custom Field, or meta key).

Example 1: Full access for anyone that is logged in:

[s2If is_user_logged_in()]
    Content for anyone that is logged in, regardless of their Membership Level.
[/s2If]

[s2If !is_user_logged_in()]
    Some public content. They're NOT logged in.
        A leading !exclamation means false.
[/s2If]

Example 2: Full access for any Member with a Level >= 1:

[s2If current_user_can(access_s2member_level1)]
    Some content for Members who are logged in with an s2Member Level >= 1.
[/s2If]

[s2If !current_user_can(access_s2member_level1)]
    Some public content.
[/s2If]

Example 3: Specific content for each different Member Level:

[s2If current_user_is(s2member_level4)]
    Some premium content for Level 4 Members.
[/s2If]

[s2If current_user_is(s2member_level3)]
    Some premium content for Level 3 Members.
[/s2If]

[s2If current_user_is(s2member_level2)]
    Some premium content for Level 2 Members.
[/s2If]

[s2If current_user_is(s2member_level1)]
    Some premium content for Level 1 Members.
[/s2If]

[s2If current_user_is(s2member_level0)]
    Some content for Free Subscribers.
[/s2If]

[s2If !current_user_can(access_s2member_level0)]
    Some public content.
[/s2If]

Example 4: Simple Conditionals w/ integrated use of [s2Get /]:

[s2If current_user_is(s2member_level1)]
    Content for Members at exactly Level# 1, on this Blog.
[/s2If]

[s2If current_user_is(s2member_level2) OR current_user_is_for_blog(24,s2member_level2)]
    They are either a Level #2 Member on this Blog,
    OR ... they're at Level# 2 on Blog ID# 24 (i.e. Multisite Networking)

        * Note the use of `OR` here. True if either condition is met.
[/s2If]

[s2If current_user_is(s2member_level3) OR current_user_is(s2member_level4)]
    Content for Level #3 - OR - Level #4 Members. Either/or.

    Hi there [s2Get constant="S2MEMBER_CURRENT_USER_DISPLAY_NAME" /].
    You have [s2Get constant="S2MEMBER_CURRENT_USER_ACCESS_LABEL" /].

        ^ This uses the s2Get Shortcode to retrieve the value of s2Member API Constants.
            These are also documented under: `Dashboard → s2Member → API Scripting`.

        So, this might come out to something like:
            `Hi there John.
            You have Gold Membership.`

        Here is a Custom Field value:
        [s2Get user_field="country_code" /]
        Custom Registration/Profile Field with the ID: `country_code`.
[/s2If]

Example 5: Inside a Text Widget:

This example uses the is_page() Conditional Tag in DynamicSite. In this example we only display content in this Text Widget to Members at Level 1 or higher—and only on a specific Page with the Slug: members-only. This example also demonstrates nested Shortcode Conditionals. Notice that NESTED Conditionals require a preceding underscore (i.e., _s2If__s2If___s2If). You can go up to three levels deep (e.g., ___s2If).

[s2If is_page(members-only)]
    [_s2If current_user_can(access_s2member_level1)]
        We're on a specific Page of the site, with Slug: `members-only`.
        Some content for Members who are logged in with an s2Member Level >= 1.
    [/_s2If]
[/s2If]

You can also accomplish this with the Widget Logic plugin (compatible with s2Member). However, the syntax is slightly different; it uses 100% PHP syntax. See also: Menu Items Visibility Control if you’d like more control over menu items.


Example 6: Conditional Tags that accept arrays:

This example uses the is_page() Conditional Tag in DynamicSite (which accepts array values too). In this example we only display content in this Text Widget to Members at Level 1 or higher—and only on specific Pages (plural). This example also demonstrates nested Shortcode Conditionals. Notice that NESTED Conditionals require a preceding underscore (i.e., _s2If__s2If___s2If). You can go up to three levels deep (e.g., ___s2If).

[s2If is_page({members-only,my-profile,123,994})]
    [_s2If current_user_can(access_s2member_level1)]
        We're on one of these pages: `members-only`, `my-profile`, Page ID `123` or `994`.
        Some content for Members who are logged in with an s2Member Level >= 1.
    [/_s2If]
[/s2If]

You can also accomplish this with the Widget Logic plugin (compatible with s2Member). However, the syntax is slightly different; it uses 100% PHP syntax. See also: Menu Items Visibility Control if you’d like more control over menu items.


Example 7: Using Conditionals together with other Shortcodes:

This example also demonstrates how to test for access to Custom Capabilities.

[s2If current_user_can(access_s2member_ccap_free_gift)]
    This Member can access Custom Capability: `free_gift`.
    Hi there [s2Get constant="S2MEMBER_CURRENT_USER_DISPLAY_NAME" /]!
        ~ Claim your free gift by clicking here.
[/s2If]

[s2If !current_user_can(access_s2member_level4)]
    Content for someone who does NOT have Level #4 access.
   [s2Member-PayPal-Button modify="1" level="4" ra="49.95" desc="Upgrade To Level #4" /]
[/s2If]

[s2If current_user_is(administrator)]
    Content specifically for a DynamicSite® Administrator.
[/s2If]

[s2If current_user_is(editor)]
    Content specifically for a DynamicSite® Editor.
[/s2If]

[s2If current_user_is(author)]
    Content specifically for a DynamicSite® Author.
[/s2If]

[s2If current_user_is(contributor)]
    Content specifically for a DynamicSite® Contributor.
[/s2If]

[s2If current_user_is(subscriber)]
    Content specifically for a DynamicSite® Subscriber.
[/s2If]

Example 8: Testing for Custom Capabilities

This example demonstrates how you might test for specific Custom Capabilities to show different content to different users. See also: Using Custom Capabilities as Product Identifiers.

[s2If current_user_can(access_s2member_ccap_monthly)]
   You are on a monthly plan.
[/s2If]
[s2If current_user_can(access_s2member_ccap_yearly)]
   You are on a yearly plan.
[/s2If]

Example 9: If/ElseIf Conditional

This example demonstrates an if/elseif conditional, by nesting an _s2If shortcode inside the [else] portion you gain more control and are able to create a more reliable conditional.

For example, if you’re using Custom Capabilities to track which subscription plan a user has (e.g., Monthly vs Yearly; see Using Custom Capabilities as Product Identifiers), and you’re also using Custom Capabilities to track which course or product a user purchased, you could use an if/elseif conditional to ensure, for example, that only users who are Yearly subscribers and have purchased Product A are able to see some specific content:

[s2If current_user_can(access_s2member_ccap_monthly)]
  You are on a monthly plan.
  [else]
  [_s2If current_user_can(access_s2member_ccap_yearly) AND current_user_can(access_s2member_ccap_product_a)]
     You are on a yearly plan AND you purchased Product A.
  [/_s2If]
[/s2If]

If you were writing this same conditional with PHP, it would look like this:

<?php if(current_user_can('access_s2member_ccap_monthly')): ?>
  You are on a monthly plan.
<?php elseif(current_user_can('access_s2member_ccap_yearly') && current_user_can('access_s2member_ccap_product_a')): ?>
 You are on a yearly plan AND you purchased Product A.
<?php endif; ?>

s2Member Supports All DynamicSite Conditional Tags

s2Member supports ALL Conditional Tags in DynamicSite—plus a few extra variations that are made possible by s2Member.

Including, but not limited to:

  • is_user_logged_in()
  • is_user_not_logged_in()
  • user_is(user_id, role)
  • user_is_not(user_id, role)
  • user_can(user_id, capability)
  • user_cannot(user_id, capability)
  • current_user_is(role)
  • current_user_is_not(role)
  • current_user_can(capability)
  • current_user_cannot(capability)
  • current_user_is_for_blog(blog_id,role)
  • current_user_is_not_for_blog(blog_id,role)
  • current_user_can_for_blog(blog_id,capability)
  • current_user_cannot_for_blog(blog_id,capability)
  • is_multisite()
  • is_main_site()
  • is_super_admin()
  • is_admin()
  • is_blog_admin()
  • is_user_admin()
  • is_network_admin()
  • is_404()
  • is_home()
  • is_front_page()
  • is_comments_popup()
  • is_singular(ID|slug|{slug,ID})
  • is_single(ID|slug|{slug,ID})
  • is_page(ID|slug|{slug,ID})
  • is_page_template(file.php)
  • is_attachment()
  • is_feed()
  • is_trackback()
  • is_archive()
  • is_search()
  • is_category(ID|slug|{slug,ID})
  • is_tax(taxonomy,term)
  • is_tag(slug|{slug,slug})
  • has_tag(slug|{slug,slug})
  • is_author(ID|slug|{slug,ID})
  • is_date()
  • is_day()
  • is_month()
  • is_time()
  • is_year()
  • is_sticky(ID)
  • is_paged()
  • is_preview()
  • is_comments_popup()
  • in_the_loop()
  • comments_open()
  • pings_open()
  • has_excerpt(ID)
  • has_post_thumbnail(ID)
  • is_active_sidebar(ID|number)

[s2If /] Advanced Tips & Tricks

  • True/false example:
    • [s2If current_user_can()][/s2If]
    • or false [s2If !current_user_can()][/s2If] i.e., the ! tests for a false value.
  • False explicitly example:
    • [s2If current_user_cannot()][/s2If] This is a special addition provided by s2Member that makes false testing easier for some.
  • Passing an ID example:
    • [s2If is_page(24)][/s2If] For functions that require an argument in the form of an ID.
  • Passing a Slug example:
    • [s2If is_page(my-cool-page)][/s2If] For functions that require an argument in the form of a slug.
  • Passing an Array example:
    • [s2If is_page({my-cool-page,24,about,contact-form})][/s2If] i.e., to pass an array of values, please use comma-delimitation.
  • Warning: Do not use spaces in your Simple Conditional arguments. The following example is INVALID:
    • [s2If is_page(My Membership Options Page)][/s2If] Use slugs or IDs instead, no spaces please—spaces will break the shortcode parser.
  • Warning: Do not use double or single quotes around arguments. The following example is INVALID:
    • [s2If is_page('my-cool-page')][/s2If] Using double or single quotes will break the shortcode parser.

Implementing AND/OR Conditional Expressions

Tip: do not mix AND/OR expressions together in a single expression.

  • The following example is INVALID:
    • [s2If is_user_logged_in() AND is_page(1) OR is_page(2)][/s2If] Use one or the other; do not mix AND/OR together. If you need to have both types of logic use nesting.

This example demonstrates nested Shortcode Conditionals. Notice that NESTED Conditionals require a preceding underscore (i.e., _s2If__s2If___s2If). You can go up to three levels deep (e.g., ___s2If).

[s2If is_user_logged_in() AND current_user_is(s2member_level1)]
  [_s2If is_page(1) OR is_page(2)]
   Content appears here.
  [/_s2If]
[/s2If]

Implementing an “else” Condition

Sometimes you might want to check for a condition, but if that condition is false, you want to do something else by default (thus, an else condition). This is a bit tricky if you’re not already familar with conditional logic, but the example below should serve you well. To accomplish this with Simple Shortcode Conditionals you combine [s2If][/s2If] with a nested [else] tag.

[s2If current_user_can(access_s2member_ccap_music)]
    Content for members with the Custom Capability: music
    [else]
        You will need to pay for access to music please.
[/s2If]

Straight-Up PHP Conditional Tags (e.g., <?php if(): ?>)

If you’re a developer and you already have experience with PHP tags, you might have noticed that all of these [s2If][/s2If] examples are relying upon PHP functions in the DynamicSite content management system. Understanding this, it becomes obvious that you could also bypass the use of [s2If][/s2If] in favor of regular PHP tags—if you wanted to. This can be easier in some cases, but really it’s a personal preference 🙂

<?php if(current_user_can('access_s2member_ccap_music')): ?>
    Content for members with the Custom Capability: music
<?php else: // Else show this. ?>
    You will need to pay for access to music please.
<?php endif; ?>