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.
Dick – I use sidebar login and register plus plugins to allow users to register as ‘subscribers’ only. Works great.
Justin, you da man!!!!
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 .
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/
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
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?
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
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.
hi
can anyone let me know what code above i can use to hide specific contet if user logged in (loged in form) but another of user is logged in (hidden text)
let say following senario:
I have a chunk of text i want to hide, in its place i want to show log in form
when user have logged in i want the form to be hidden and the chunk of text that were hidden to be shown
any idea ?
Hi Justin
I’m using the Mystique Theme which contains your member shortcodes. Everything works ok and the code is an exact copy as above. I have only one little problem, I’m trying to run another shortcode inside yours which does not seem to work. Is that possible at all and if so, how?
hey man.. thanks a lot!
but can you help me a bit more?
i want to show the shortcodes in a private page
to the website owner and what ever i do
to display the shortcode as a user manul failes
no matter if i wrap it in or
the actuall result of the shortcode apears
instead of the explenation how to..
how do you show [shortcode] without it actually
activating the code? much like you did here
p.s you should realy add a “notify me on new comments”
so it would be easier to return to the page when respond published
Justin, this code is just the best, thanks for sharing.
I have now setup some roles, added the shortcode to my template, and now have a widget area only for members who are agents.
Such a clean and simple solution.
You’re an absolute legend, thanks for sharing.
Nice idea, comes very handy at times.
Do you have any idea for displaying certain widgets to logged in members only?
Hi Justin, thanks for your great job.
I found this old article that is very good for me.
Only a question: is there a way to insert a shortcode (for example a form made by cforms or gravity) into the [member] shortcode? I tried by myself but it doesn’t work, wp prints the shortcode as is and not the form.
Thank you very much
Enrico
Solved! I am not a programmer, but it seems to work.
Changed:
return $content;
with:
return do_shortcode($content);
I hope could be useful for others!
Not cool —
Link removed by the administrator.
Yeah, it’s just something you learn to live with.
Ian you just made me look like a coding god whilst on the phone to a client… searched for a solution to showing member content, cut and pasted your code into my functions.php, stuck the shortcode around a cform and hey presto… it worked. all in about 60 seconds. many thanks!
Thanks for the great tip Justin.
It works great, but like stonecoldcnc above, I can’t get the Gravity Forms shortcode to work. Do you know a simple way to fix this?
Thank,
Joakim
Thanks for this great tutorial.
It has been very help full for me.
Thanks a lot.
Hi
Excellent article.
But to my horror, I find that your post’s content has been verbatim scraped and plagiarized in another blog where I landed after a search for the relevant keywords. I do not want to provide a direct link to the web page to avoid directing traffic to that site unwittingly! It is livexp dot net and the title is exactly this post’s title.
Regards
S.K
great justin brother….i like ur every post.
Perfect, but, i don’t uderstand why this text that appear for non registered users (This is members-only content.) have the pre and code tag HTML?
Great! I have searched long after how it was possible. Have taken it in use and it works well.
However, a small problem turned out
On the pages I use the code I can not use other [code] of any kind. for example. [content mirror site = undefined record type = page item = 138]
The code is seen simply as plain text on the sides, and not the expected function
Do you have a solution to this
Hi Justin,
I was trying to make a shortcode that displays user profile information. I followed your tutorial on creating custom fields in user profile. I wanted to know how to display such custom themes inside the shortcode so they can be placed for memberonly content page.
Your help will be much appreciated.
Hi Justin, this is a great little shortcode, but for some reason, content within the [member] tags is showing up in the excerpts of my search results.
If you check my site here: http://www.emaginetestbed.com/oicci/page/1/?s=Invest
the posts with telephone numbers and addresses should all be hidden to non-members. On the actual page where this info is placed, the content within those tags is hidden.
Any idea why this should happen and how I can fix it?
For the “[member]” function, it would be great if we could set an optional custom message for visitors who are not logged in. So – there can be a default message, but anywhere you want to customize the message for that page/content, you can.
Thank you for this information however, I do have a question.
Is there anyway to create a custom login page?
Thanks in advance!
Michael
You just saved me couple of hours. Thanks a lot. Justin you ROCK
Hi. I have a problem with shortcodes.
I need put one shortcode ([contact form]) in to second shortcode ([member]).
In oder words, i need only registered users can show contact form.
Is it possible?
Hi, this is great shortcode! One part I cannot seem to get to work is:
return ‘Sorry, but you cannot access this content without…’;
It shows nothing. Any ideas?
Fabulous…is it possible to add check boxes to select and toggle several separate elements of content so only those check boxes selected show only their corresponding content elements…?
Great shortcode! For those of you who like me where wondering how to make the code work with a shortcode inside of it. Its really simple just change.
to