The Professional Membership Plugin for WordPress

 

Shortcodes & More to Manage Your Content Wisely

The [private][/private] tags are a great way to protect your content and control which users have access to it, but it’s not the only way to protect your content. In fact, there are a number of variations on the private tag that allow you to do all kinds of neat things with your content.

Protection Shortcodes

Used when writing posts, pages, and other content types, protection shortcodes are a quick and easy way to control the visibility of your content. They work inside the “loop” and are inserted into the content in the appropriate locations while looking at the HTML view in WordPress. Otherwise, you’ll end up with funky code clearly visible to all visitors in your content.

(You can toggle between HTML and Visual views on the editor. There are also little monkey buttons that will automatically enter the code for you.)

Private Tag


Public content. [private]Protected content goes here.[/private] Public content continues here.

The content between the tags will be hidden from everyone, except those who belong to the package account types (subscription levels) you’ve chosen. If anyone who doesn’t belong to that particular level tries to view the content, they’ll receive the default error message.

You can modify the default error message by going to your admin panel, going to “Your Members”, then “Other Settings” and finally to “Messages”.

Just want to modify the default error message for a specific post or page? Simple add:

[private override_message="Your message to the user would go here."]

And you’ll be happy to know the override message can contain HTML, so you can include links and all sorts of other elements to help you upsell, convert, and otherwise improve your business.

No Access Tag

[no_access] isn’t used often, but it can do some pretty fantastic things. Most importantly, this tag makes it possible for you to create and serve up dedicated alternative content for users who don’t have permission to view the original content. This means those with access permission won’t see anything. (Post authors, editors, and those with a higher role will see the content, but it will be highlighted in pink, so you can tell the difference.) Think of it like the opposite of a private tag. We should add that the no_access tag will also show in RSS feeds by default.

So, for example,

[no_access] We're sorry you can read more about this amazing product and learn our deepest darkest secrets. Want to know what they are? Buy a subscription here[/no_access]

Private Logic Tag

Private logic tags allow users to access the content, but only if the user has access to the page and/or an alternate page.

For example:

[private_and #1]Only people who have access to this post and the post with the ID# 1 can see this content[/private_and].

This means only users with a specific subscription and the page ID # can see this content. (Post ID # is found in the post URL when you hover over the title on the Posts page in your WordPress Admin. It looks like: ?post=1234 The ID# for that post would be 1234.)

Want to give permission to view the content if they have access to the post, as well as to users who only have the ID#? Use [private_or] instead of [private_and].

Users who try to access that content without meeting either (or both) of those requirements will receive the default message (mentioned previously).

This is a great tag if you want to reward members who have purchased a few years worth of access, for example, but not those who only paid for 3 months of the same level of access.

User Has Access Tag

Content found between the user_has_access tags will reveal content only to those with access to that particular post/page specified in the tag. If not, they’ll see nothing.

[user_has_access #123]Content here is only shown to users with access to post #123[/user_has_access]

One great use for this tag is if you have tables of content that should only show content the user actually has access to.

User Account Type Tag

This is the most flexible of the content tags. It doesn’t return data or any kind of error if the user doesn’t have access to the content. This time, it shows content if the user belongs to a certain account type. (Note: When adding the account type to the tag, use all lower case and substitute spaces for the “+” sign.


[ym_user_is package_type="basic"]Will reveal this content to basic users[/ym_user_is]

OR

[ym_user_is package_type="gold member"]Will reveal this content only to gold members[/ym_user_is]

Sidebar Widget

Your Members Text Widget

If you visit the widget section in your WordPress admin panel, you’ll find a widget called Your Members text. This is a multi widget you can use repeatedly. What does it do? You can add content specifically for each package account type by selecting one or more of the account types listed in the bottom of the widget. PHP can also be used within the widget if embraced with php tags.

Tip: You can combine this widget with our free Shortcode Widget Plugin for even more flexibility within a post and/or page.

Protecting a Sidebar Region

Sidebar Regions are parts of your theme which are “widgetable” within the code they look like:


if ( ! dynamic_sidebar( 'name' ) ) :
content
endif; // end primary widget area

It might also look like:


if ( is_active_sidebar( 'name' ) ) :

So, if you want to protect the widget area and reveal it only to certain post, you would use:


if(ym_user_has_access(xx)) {
if ( ! dynamic_sidebar( 'name' ) ) :
content
endif; }

The xx in the code is the post ID they need to have access to. And the good news is, you can use all of the codes below with this to get the most from Your Members.

Theme Functions

These functions can be used right inside your theme to protect content not inside a widget or the loop. Keep in mind these are php functions and so need to be wrapped inside php tags.

User Has Access

This tag protects content and controls access.

Basic usage:


if(ym_user_has_access()){
thing to protect
}

Outside the loop, however, it won’t know what post you’re on, so you’ll need to call the ID first, like this:


global $wp_query;
if(ym_user_has_access($wp_query->post->ID)){
thing to protect
}

Note: You’ll only need to call global $wp_query once within the theme. Unless its help in a function, you can specify any post ID to go in there. So, for example, if we want people to have access to the protected area, only if they have access to post 12


if(ym_user_has_access(12)){
thing to protect
}

You can also specify a user_id Just be aware that doing so will override the current user, so use it wisely.


if(ym_user_has_access(false,1)){
thing to protect
}

Would for example use the permissions of user 1 when looking to see if user should access

User Account Type

To determine a user’s account type, use:


$account_type = ym_get_user_account_type();

So, for example, if we wanted to protect content so that only basic account members could see it, we would use:


$account_type = ym_get_user_account_type();
if($account_type == 'basic'){
content here
}

Like the user_has_access code, you can specify an ID, if you want to check the access level of a third party.

Is Post Protected

Doing something special and need to know if a post is protected? Use:


if(ym_is_post_protected()){
//post is protected do something
}

Like the ym user_has_access code, you will need to give either a post ID, or if used outside the loop, $wp_query->post->ID

This is handy for calling javascript files inside the head on load for protected posts, for example.

Using Custom Fields

To call a user’s custom fields you can use:


ym_custom_fields = ym_get_custom_field_array($user->ID);

This returns an array of all their fields. To check against say name, you can then:


ym_custom_fields = ym_get_custom_field_array($user->ID);
if($ym_custom_fields['fname'] == 'tim'){
//hello tim
}

Of course, you can always just…


ym_custom_fields = ym_get_custom_field_array($user->ID);
echo 'hello '.$ym_custom_fields['fname'];

Your Members Theme Integration — Best Practices

Place Everything Into a Function File

Try to keep Your Members logic in the function file, away from the templates themselves. This makes them easier to find, but it also means you’re keeping them inside the function when calling global variables.

Check If Your Members Is Installed

A best practice method for checking if Your Members is installed is:


function is_ym_installed(){
global $ym_sys;
if(!$ym_sys){
return false;
}
return true;

This documentation is part of the Your Members WordPress Membership Plugin Guides and Tutorials.