Post Pic

WordPress shortcode: Display content to registered users only

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

May 13 2009 15:10

There are not a lot of blogs with members, but for those who use the function is useful.

May 13 2009 16:58

How about the RSS, will the content be hidden from the feed to?

May 13 2009 19:23

Indeed, this can be very handy function for those that have big members base, and want to publish only content for them.

May 14 2009 06:06

Very cool goes well with the code you showed us for showing Adsense to SE visitors only to avoid smart pricing.

May 14 2009 11:01

@Erik Pettersson: Yes :)

May 15 2009 05:08

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?

May 16 2009 03:45

the information is very nice, thank you so much

May 26 2009 03:30

Is there a way to inform the readers that the article is in short version, so they need to register to continue reading?!!

May 27 2009 18:23

This php code is really awesome and i think it is true useful code thanks for sharing this with us.

Jun 02 2009 09:22

Hmm. Nice for those who uses register function. The contents for those goes more authenticated this way. Thanks.

Jun 02 2009 11:15

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?

Jun 02 2009 13:00

@Matthew Wayne Selznick

Yes it is possible using this method, it would just take some tweaking of the code in functions.php

Jun 05 2009 13:57

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

Jun 07 2009 19:26

I would definitely use your provided php code. Becoz it seem to me that it will definitely work. Thanks and keep updating your blog..

Jun 21 2009 04:13

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).

Jun 21 2009 04:19

Never mind!! It´s working !!!

Aug 29 2009 00:34

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.

Sep 01 2009 06:51

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?

Nov 30 2009 19:12

This is perfect!

Dec 21 2009 00:10

Very useful post. I just added it to my Wordpress blog and it is working great. It save a lot of typing.

Jan 04 2010 16:15

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?

Jan 20 2010 12:21

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 ”;
}

Feb 03 2010 19:25

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:

Leave a Comment

* Name, Email, Comment are Required