
Ever wanted to be able to show some content only to registered users in your blog posts? If yes, just read this recipe, which contains an awesome shortcode to do it!

Ever wanted to be able to show some content only to registered users in your blog posts? If yes, just read this recipe, which contains an awesome shortcode to do it!
Just paste the following code on your 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 '';
}
Once done, you can add the following to your posts to create a portion or text (or any other content) that will be only displayed to registered users:
[member] This text will be only displayed to registered users. [/member]
That's all. Easy and useful!
Credits goes to Justin Tadlock for this awesome recipe!
35 Responses
There are not a lot of blogs with members, but for those who use the function is useful.
How about the RSS, will the content be hidden from the feed to?
Indeed, this can be very handy function for those that have big members base, and want to publish only content for them.
Very cool goes well with the code you showed us for showing Adsense to SE visitors only to avoid smart pricing.
@Erik Pettersson: Yes
This is *so* close to something I’m trying to figure out!
Is there a way (or perhaps a plugin already in existence) to hide certain menu items *and* pages from non-registered members of a site?
the information is very nice, thank you so much
Is there a way to inform the readers that the article is in short version, so they need to register to continue reading?!!
This php code is really awesome and i think it is true useful code thanks for sharing this with us.
Hmm. Nice for those who uses register function. The contents for those goes more authenticated this way. Thanks.
Will this display some kind of text like “the following is only for registered users” or do I have to manualy add it at the end of every post?
@Matthew Wayne Selznick
Yes it is possible using this method, it would just take some tweaking of the code in functions.php
And how could be that code then? It would be really helpful if instead of nothing to be shown a text like “only for registered users”.
Thank you
I would definitely use your provided php code. Becoz it seem to me that it will definitely work. Thanks and keep updating your blog..
Does it work with WPMU? In my test site it seems to work just fine for the “primary blog”. But for the member´s blog it is not working (nothing is showing when I´m logged in).
Never mind!! It´s working !!!
let say you want to display a download link only for registered users.
above hack will be like this:
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 ‘Only member can see this URL. Become a member now!.’;
}
Then create a post and write like this:
Download URL:
[member]
http://hecode.com/wp-content/uploads/2009/07/Are-you-doing.jpg
[/member]
love you all.
This code is great but how would I make it so it works with post excerpts? I’d like the same message “Only members can see this URL…” to replace the post excerpt. Any ideas?
This is perfect!
Very useful post. I just added it to my Wordpress blog and it is working great. It save a lot of typing.
Hi there! Very nice functionality!
I need the same for GUEST user… I tryed this code:
add_shortcode( ‘guest’, ‘guest_check_shortcode’ );
function guest_check_shortcode( $atts, $content = null ) {
if ( !is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return ”;
}
… but it doesn’t work… any idea?
medusacross: it works perfectly for me,
add_shortcode( ‘nonmember’, ‘non_member_check_shortcode’ );
function non_member_check_shortcode( $atts, $content = null ) {
if ( !is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return ”;
}
What if i want to use let’s say [ nggallery id=1 template=sample1 ] inside [member]? How can i make it? It is showing me the code now no gallery. Thanks!
Trackbacks: