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.
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!!
just return something other within return ‘some text’; and not blank should do the trick.