latest stable versions: v140816 (changelog)

Simple Shortcode Conditionals

A Quick Overview — Simple Shortcode Conditionals

s2Member® makes it very simple to protect entire Posts/Pages/Categories/Tags/URIs/etc. This can be accomplished from the WordPress® Dashboard, under: s2Member® -› Restriction Options. Or, from your Post/Page editing station in WordPress®. We consider this to be point-and-click functionality. Very easy.

What many site owners don’t realize though, is that s2Member® also makes it pretty simple to protect “parts” of a Post or Page. You can even get creative about what you display to certain Users/Members, based upon your own custom criteria. s2Member’s Simple Shortcode Conditionals are the key to accomplishing this.

In fact, to say that s2Member® can merely protect “parts” of a Post or Page, is really an understatement. With s2Member’s Simple Shortcode Conditionals, you can not only protect parts of your content, you can also deal with other kinds of conditions; making these a vital part of a successful membership site.

A Few Scenarios where Simple Shortcode Conditionals Can Help

  • Sometimes it’s necessary to allow everyone to access to your content (i.e. everyone can land on a certain Post or Page and view it); but maybe you need to hide (protect) a certain part of that content; or maybe you need to display something different to non-Members.
  • Maybe you need to change the way something appears (or even what details are given), based on the current Post or Page, the current Post Type; or based on what Tags that content may have; what Category it’s in, what Sidebar it’s in, if it’s Sticky or not, who authored the content, etc.
  • Maybe you want to display certain data, but only if a User/Member has previously completed an action. Such as joining your mailing list, filling out a survey, visiting a certain Post or Page, filling in a value for a certain Profile Field, clicked a certain link, etc, etc.
  • In some cases, you might want to display something different, depending on the time of day it is; or depending upon how long an existing Member has been paying you. This would be considered Content Dripping, and Simple Shortcode Conditionals can help with this too.
There are many situations like this. Obviously, s2Member® can’t possibly predict every possible scenario — in every possible business model. But what we CAN do is provide the tools you need to make things like this easier to deal with.

Simple Shortcode Conditional Tags (Commonly Used w/s2Member®)

s2Member makes Simple Conditionals available to you from within WordPress®, using Shortcodes that are fully compatible with both the Visual Editor, and also the HTML Tab in WordPress®. In this section, we’ll demonstrate several functions that are possible using Shortcodes.

I will list a few here, but you can also take a look at Conditional Tags available for WordPress® in general. There are MANY possibilities — well beyond the examples given here.

Conditional Tags Commonly Used In Conjunction with s2Member®
is_user_logged_in(), user_is(user_id, role), user_can(user_id, capability), current_user_is(role), current_user_can(capability), current_user_is_for_blog(blog_id,role), current_user_can_for_blog(blog_id,capability), is_super_admin(user_id)

Most of these Conditional Tags work together with s2Member® & WordPress® Roles/Capabilities. It would be a good idea to learn more about WordPress® Roles/Capabilities, so you can understand exactly what you’re working with. The examples below will serve you well, but the article on WordPress® Roles/Capabilities is recommended if you’re having trouble.

To make use of these Conditional Tags, please follow the code samples below. Using Shortcodes for WordPress®, it’s easy to build Simple Conditionals into your content. With Simple Shortcode Conditionals, you can accommodate many different scenarios; all based on a Member’s Level, Custom Capabilities — or other factors.

s2Member’s Shortcodes can be used inside a Post/Page, and also inside Text Widgets.

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 WordPress®. 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 (___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

Example #6 — Conditional Tags that accept arrays.

This example uses the is_page() Conditional Tag in WordPress® (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 (___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

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 WordPress® Administrator.
[/s2If]

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

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

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

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

s2Member® Supports All WordPress® Conditional Tags
s2Member supports ALL Conditional Tags in WordPress®
— 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)

Passing Arguments Into A Simple Conditional Tag

1. True/false -› ex: current_user_can() or false !current_user_can()
2. False explicitly -› ex: current_user_cannot()
3. Passing an ID -› ex: is_page(24)
4. Passing a Slug -› ex: is_page(my-cool-page)
5. Passing an Array -› ex: is_page({my-cool-page,24,about,contact-form})

*Tip: do NOT use spaces in your Simple Conditional arguments.
INVALID: [s2If is_page(My Membership Options Page)]
Use slugs or IDs instead, no spaces please — spaces will break the Shortcode parser.

Implementing AND/OR Conditional Expressions

*Tip: do NOT mix AND/OR expressions.
INVALID: [s2If is_user_logged_in() AND is_page(1) OR is_page(2)]
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 (___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]

s2Member® Roles/Capabilities (Including bbPress® Support)

Membership Levels Provide Incremental Access
  • A Member with Level 4 access, will also be able to access Levels 0, 1, 2 & 3.
    WordPress® Role: s2member_level4

    WordPress® Capabilities:
    read, level_0
    access_s2member_level0
    access_s2member_level1
    access_s2member_level2
    access_s2member_level3
    access_s2member_level4

  • A Member with Level 3 access, will also be able to access Levels 0, 1 & 2.
    WordPress® Role: s2member_level3

    WordPress® Capabilities:
    read, level_0
    access_s2member_level0
    access_s2member_level1
    access_s2member_level2
    access_s2member_level3

  • A Member with Level 2 access, will also be able to access Levels 0 & 1.
    WordPress® Role: s2member_level2

    WordPress® Capabilities:
    read, level_0
    access_s2member_level0
    access_s2member_level1
    access_s2member_level2

  • A Member with Level 1 access, will also be able to access Level 0.
    WordPress® Role: s2member_level1

    WordPress® Capabilities:
    read, level_0
    access_s2member_level0
    access_s2member_level1

  • A Subscriber with Level 0 access, will ONLY be able to access Level 0.
    WordPress® Role: subscriber

    WordPress® Capabilities:
    read, level_0
    access_s2member_level0

  • A public Visitor will have NO access to protected content.

WordPress® Subscribers are at Membership Level 0. If you’re allowing Open Registration, Subscribers will be at Level 0 (a Free Subscriber). WordPress® Administrators, Editors, Authors, and Contributors have Level 4 access, with respect to s2Member. All of their other Roles/Capabilities are left untouched.

Membership Levels Provide Incremental Access

s2Member configures your Membership Roles (by default, these include: s2Member Level 1, s2Member Level 2, s2Member Level 3, s2Member Level 4), with a default set of bbPress® permissions that allow all Members to both spectate and participate in your forums, just as if they were a WordPress® Subscriber Role (and/or a bbPress® Participant Role).

bbPress® also adds some new Roles (dynamic Roles in bbPress® 2.2+) to your WordPress® installation. These include but are not limited to: Keymaster and Moderator. s2Member allows Forum Keymasters & Moderators full access to the highest Membership Level you offer; just like it does with Administrators, Editors, Authors, and Contributors.

  • A Member with Level 4 access, will also be able to access Levels 0, 1, 2 & 3.
    Members with Level 4 access can also spectate/participate in bbPress® Forums.
    WordPress® Role: s2member_level4

    WordPress® Capabilities:
    read, level_0
    access_s2member_level0
    access_s2member_level1
    access_s2member_level2
    access_s2member_level3
    access_s2member_level4

    bbPress® Capabilities:
    spectate
    participate
    read_private_forums
    publish_topics
    edit_topics
    publish_replies
    edit_replies
    assign_topic_tags

  • A Member with Level 3 access, will also be able to access Levels 0, 1 & 2.
    Members with Level 3 access can also spectate/participate in bbPress® Forums.
    WordPress® Role: s2member_level3

    WordPress® Capabilities:
    read, level_0
    access_s2member_level0
    access_s2member_level1
    access_s2member_level2
    access_s2member_level3

    bbPress® Capabilities:
    spectate
    participate
    read_private_forums
    publish_topics
    edit_topics
    publish_replies
    edit_replies
    assign_topic_tags

  • A Member with Level 2 access, will also be able to access Levels 0 & 1.
    Members with Level 2 access can also spectate/participate in bbPress® Forums.
    WordPress® Role: s2member_level2

    WordPress® Capabilities:
    read, level_0
    access_s2member_level0
    access_s2member_level1
    access_s2member_level2

    bbPress® Capabilities:
    spectate
    participate
    read_private_forums
    publish_topics
    edit_topics
    publish_replies
    edit_replies
    assign_topic_tags

  • A Member with Level 1 access, will also be able to access Level 0.
    Members with Level 1 access can also spectate/participate in bbPress® Forums.
    WordPress® Role: s2member_level1

    WordPress® Capabilities:
    read, level_0
    access_s2member_level0
    access_s2member_level1

    bbPress® Capabilities:
    spectate
    participate
    read_private_forums
    publish_topics
    edit_topics
    publish_replies
    edit_replies
    assign_topic_tags

  • A Subscriber with Level 0 access, will ONLY be able to access Level 0.
    Members with Level 0 access can also spectate/participate in bbPress® Forums.
    WordPress® Role: subscriber

    WordPress® Capabilities:
    read, level_0
    access_s2member_level0

    bbPress® Capabilities:
    spectate
    participate
    read_private_forums
    publish_topics
    edit_topics
    publish_replies
    edit_replies
    assign_topic_tags

  • A public Visitor will have NO access to protected content (and no access to bbPress® Forums).

WordPress® Subscribers and bbPress® Spectators/Participants are at Membership Level 0. If you’re allowing Open Registration via s2Member, Subscribers will be at Level 0 (a Free Subscriber).

WordPress® Administrators, Editors, Authors, Contributors, and bbPress® Keymasters/Moderators have Level 4 access, with respect to s2Member. All of their other Roles/Capabilities are left untouched.

Modifying s2Member® Roles/Capabilities

If you would like to modify the default Roles/Capabilities that s2Member® works with, you might give this plugin a shot. Please see this plugin at WordPress.org: Enhanced Capability Manager.

However (please NOTE): s2Member® Support Reps will NOT offer support for a site that is running modified Roles/Capabilities. If you customize your installation, you will be responsible for debugging anything that may not be working as expected. Please see our Support Policy for some clarification on this.

If you DO modify your Roles/Capabilities, please be sure to install this hack to prevent s2Member® from resetting your customization in the future, should you attempt to update your copy of s2Member® or s2Member® Pro. This will lock-in your customizations; telling s2Member® NOT to reset your Roles/Capabilities during future plugin updates.

Please create this directory and file:
/wp-content/mu-plugins/s2-hacks.php
(NOTE: these are MUST USE plugins, see: http://codex.wordpress.org/Must_Use_Plugins)
(See also: http://www.s2member.com/kb/hacking-s2member/)

<?php
add_filter('ws_plugin__s2member_lock_roles_caps', '__return_true');

Other Related Articles (Suggested Reading)
See: s2Member® Roles/Capabilities (what are they?)

See: Dashboard -› s2Member® -› API / Scripting -› Advanced PHP Conditionals
See: Dashboard -› s2Member® -› API / Scripting -› Advanced PHP Query Conditionals
See: Dashboard -› s2Member® -› API / Scripting -› Custom Capabilities (Packages)

See also: WordPress® Conditional Tags and s2Member® API Functions
See also: WordPress® Roles/Capabilities and bbPress® Roles/Capabilities
See also: Video » Extending s2Member® w/ Custom Capabilities (moving away from Levels)

About the Author

Jason (Lead Developer)Yep, I'm one of those oddball coders! When I'm not coding, I'm thinking about code, or helping others with code. Heck, sometimes my dreams are in the PHP language :-) It's been a passion of mine for 12+ years now.View all posts by Jason (Lead Developer) →

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.