DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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!

6 Comments

  1. Hi! Great tutorial, but i was wondering how i can put a message for when someone tries to access that restricted area, but after being logged in the message goes away. Thanks!

  2. Got Error:
    Warning: Cannot modify header information

    How To Fix This…?!

  3. How do I use this in a php file? I have some html that contains custom fields in my single post php file and I want to hide some of that content from non-members.

    I tried wrapping the content in the [member] [/member] codes but it didnt work, any ideas

  4. Hello. 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?

  5. Awesome recipe bro!! This is simple awesome!!
    Thanks a lot! 🙂

  6. In case you want use another shortcodes inside [member], ie. [member][contact form][/member], you have to update return statement this way:


    if (is_user_logged_in() && !is_null($content) && !is_feed()) {
    return do_shortcode($content);
    } …

Leave a Comment

Your email address will not be published. Required fields are marked *