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.



This is brilliant Justin, thanks for sharing this.
You write it’s for post and pages, but would it work for widgets as well?
Great tutorial Justin. Thanks a bunch for all your contributions. Keep ‘em coming.
Thomas Clausen — By default, WordPress doesn’t run shortcodes in widgets. But, that doesn’t mean we can’t make it work that way. See this tutorial on adding shortcodes to widgets.
Ryan — Thanks. I’ve got plenty of ideas, so I’m sure I’ll keep ‘em coming for some time.
I’ve really said it but surely shortcodes are lovely
I use this plugin
http://wordpress.org/extend/plugins/hidepost/
Many — Shortcodes are nice. I’m actually using this method on Theme Hybrid too with just a couple of adjustments.
Nice — Not to say anything bad about the plugin developer, but I had a look through the plugin’s code and don’t really recommend it. It needs some serious updating if you ask me.
Wow I can see how this could be useful.
This is GREAT, but will you write a followup on how to facilitate the user signup or point me to a good source for that? Thanks.
I really like not having to depend on plugins. Thanks for posting this. I’m going to start using it right away.
Hi Justin,
Love this info – thanks so much.
Inferring that you’re not planning on doing a tutorial on the member-joining part of this process, can you suggest a resource for that code?
Thanks again,
- Kat
The Frosty — Definitely. I use this method all the time for both my projects and others.
Dick — Maybe I’ll write that one day. I’m probably not the best guy to tell you how to market your site. Everything I do is about word-of-mouth advertising. Then, I just try to provide something that others want or need.
Kevin — I’m the same way. I hate having to keep up and depend on more than a handful of plugins. I usually either create one giant plugin or add everything to my theme’s
functions.phpfile to handle all of my custom functions.Kat — I didn’t write the code for payment processing and such if that’s what you’re referring to. Check out the comments on this post about membership plugins for something in that area. I personally use Your Members.
[...] shortcodes to show member only content. The great thing about this is that you can use shortcodes in any bog standard template. So you [...]
[...] Looking to hide content to non members?By Austin on May 12, 2009 Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic.Are you looking for a better way to hide content to non-members? You may want to check this out: Using shortcodes to show members-only content [...]
[...] Justin Tadlock has another great tutorial for WordPress users, and it is how to hack/modify your WordPress template so that content on the site will only be available to registered users only. [...]
Dick – I use sidebar login and register plus plugins to allow users to register as ’subscribers’ only. Works great.
Justin, you da man!!!!
[...] Source: http://justintadlock.com/archives/2009/05/09/using-shortcodes-to-show-members-only-content [...]
I must be putting the code in the functions.php in the wrong place? I got it to hide content, but when a user goes to login there is an error:
Warning: Cannot modify header information – headers already sent by (output started at /home1/capeknin/public_html/capek9cardio/wp-content/themes/CoolWater/functions.php:32) in /home1/capeknin/public_html/capek9cardio/wp-login.php on line 287
Warning: Cannot modify header information – headers already sent by (output started at /home1/capeknin/public_html/capek9cardio/wp-content/themes/CoolWater/functions.php:32) in /home1/capeknin/public_html/capek9cardio/wp-login.php on line 299
Great tutorial Justin , you are a great blogger . I’ve seen a lot of good posts wroted by you ,it’s impressive what you can do .
[...] elegantno je rešenje sa bloga Džastina Tedloka, koji koristi tzv. shortcodes da bi prikazao određeni sadržaj samo registrovanim posetiocima [...]
[...] Shortcodes Provides a set of shortcodes that may be used to restrict or provide access to certain areas of your site from within the post editor (like the shortcodes I posted in Using shortcodes to show members-only content). [...]
Hi Justin,
That’s an elegant solution, better than using plugins with huge overhead.
Unfortunately I get the ‘Cannot modify header information’ error too.
Any suggestion how to fix it?
Thanks,
Ralf
Okay, I fixed the ‘Cannot modify header information’ error. For anybody who might have the same problem:
Make sure there is no white space outside of the php start and end tags. While a blank line before the <?php start tag may look innocent, when processed by PHP, it will turn into an echo statement printing out a blank line. This is a common culprit.
source:
http://www.tech-recipes.com/rx/1489/solve-php-error-cannot-modify-header-information-headers-already-sent/
[...] way of doing this. Fortunately I remembered an article that I read a few months back over at Justin Tadlock’s blog. I went hunting and found that the method used in Justin’s article was exactly what I was [...]
The members only code snippet worked beautifully. Thanks.
Interestingly if I use in a document, the members only code doesn’t work any more.
Does anyone know if there’s a “fix” for this.
Interestingly if I use “nextpage” in a document, the members only code doesn’t work any more.
Does anyone know if there’s a “fix” for this.
“Sorry for doubleposting, but the code for nextpage that I inserted didn’t show up.”
i’d like to see this with a little button on the wysiwyg editor so that I can highlight my posts’s text that i want hidden… much like i would to bold some text etc.
Is there any way I can get this to work with post excerpts?
Right now, if a post is within the [member] tags, nothing shows up in the excerpts. I’d like it to say something like “This post is viewable to members only”. Love the tip on the shortcodes!!
Hey, thanks for the code, I will just try this out.Definitely will be a great timesaver
[...] Using shortcodes to show members-only content [...]
[...] Using shortcodes to show members-only content [...]
[...] Using shortcodes to show members-only content [...]
[...] Using shortcodes to show members-only content [...]
[...] Source Link [...]
Fine, but if in content section you’d find a link to file?
It would be accessfull for non-logged users if they knew the link to a file… how to solve that?
[...] Justin Tadlock [...]
That was just as usefull as I need. Some people needs to hide content to their guest visitors and this post is the solution I give to somebody who asks me that.
I Love this post
I was looking everywhere for this! But im having a problem, or maybe im doing it wrong…
I did everything you described above, but when i view my page it shows the members tags [ ] in the brackets…and content is still visible. Imm working in wpmu/ BP does it still operate in mu nad bp? I assume it still would
Any help would be greatly appreciated
[...] 17.How To Show Blog Content Only To Registered Users [...]
[...] 17.How To Show Blog Content Only To Registered Users [...]
[...] 17.How To Show Blog Content Only To Registered Users [...]
[...] Source Link Related Posts:Excellent jQuery TutorialsPHP for Beginners: Building Your First Simple CMSWriting clean, secure and easy to maintain PHP codePHP Tips and Tricks10 Practical PHP Regular Expression RecipesShare/Bookmark [...]
Wow this is going to allow me to completely redo my members section. I had been using forums as my member restricted section, this will be much better.