X

How To Create a Member-Only Content Shortcode

Snippets by IsItWP

Are you looking for a way to place content that you wish your members to see using a [member][/member] shortcode? While there’s probably a plugin for this, you can use this quick code snippet to create member-only content in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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

Here’s an example of the shortcode.

[member]
This text will be only displayed to registered users.
[/member]

This snippet was created by Justin Tadlock.

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 11 best WordPress quiz plugins and how to set up Google Analytics eCommerce tracking for WooCommerce.

Comments  Leave a Reply

  1. Sweet Snippet!!
    but leaving the space empty for non-member visitors would be too boring. how about inserting some text-message for non-member visitors, like “You must be logged in to view this content” or something like that?

    Oh, and thanks for the good work!!

    1. just return something other within return ‘some text’; and not blank should do the trick.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!
[63% OFF Today!]
[63% OFF Today!]
[60% OFF Today!]
[60% OFF Today!]
[50% OFF Today!]
[50% OFF Today!]