Using shortcodes to show members-only content
I get several questions every week about setting up a membership site with WordPress. Although I’m not going over all the particulars of the process in this post, I want to show you some effective techniques that can make your life much simpler.
This tutorial will cover three methods for setting up shortcodes to use in your posts and pages that will allow you to hide or show content depending on who’s viewing it.
Content for users that are not logged in
Many people want to focus on hiding content from this group of users, but I want to start by showing them content. Most traffic to your site will likely be through non-logged in users, so make sure you give this group of people something.
Open your theme’s functions.php
file in your favorite text editor. Add this PHP code:
add_shortcode( 'visitor', 'visitor_check_shortcode' );
function visitor_check_shortcode( $atts, $content = null ) {
if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
return $content;
return '';
}
Anytime you write a post/page, add this to only show content to users that are not logged in:
[visitor]
Some content for the people just browsing your site.
[/visitor]
You should also note that this content will be added to your feeds. The next two techniques will hide content from feed readers and others on your site.
Showing content to logged-in users
Now, you’ll see how to show content only to users that are logged into your site. This will be hidden from all other users and not shown in your feeds.
Add this code to your theme’s functions.php
file:
add_shortcode( 'member', 'member_check_shortcode' );
function member_check_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
Then, all you must do is add some content in between your [member]
tags when writing a post/page like so:
[member]
This is members-only content.
[/member]
Showing content depending on user’s capability
This bit of code is my favorite because it allows me to check for users based on their capabilities. You can use something like the Role Manager plugin to create custom capabilities or just use the default WordPress capabilities to check against.
Add this code to your theme’s functions.php
file:
add_shortcode( 'access', 'access_check_shortcode' );
function access_check_shortcode( $attr, $content = null ) {
extract( shortcode_atts( array( 'capability' => 'read' ), $attr ) );
if ( current_user_can( $capability ) && !is_null( $content ) && !is_feed() )
return $content;
return '';
}
Now, we’re going to show content only for someone that has the capabilitiy of switch_themes
(an administrator in the default WordPress setup):
[access capability="switch_themes"]
The currently logged-in user is a god on this site.
[/access]
The default capability is read
in the code above, so adding this will give access to the content to any logged-in user that can read (this is likely everyone):
[access]
You can read, right?
[/access]
How to show content if user doesn’t meet requirements
These shortcodes are great if you just want to hide something. But, if you want to show a message for people that you’re hiding content from, you’ll need to make a small change.
In each of the code snippets above, the line just before the last is:
return '';
That means nothing will be shown if the user doesn’t meet the requirements defined by the shortcode. In order to leave them a message, change it to this:
return 'Sorry, but you cannot access this content without...';
Set up your members-only content
Of course, this tutorial hasn’t told you how to get users to sign up. You’ll have to figure that out all on your own. Once they do, you can show whoever whatever content you want.
This is so much nicer than having to use a PHP plugin just to check for a user’s info within a post.