A Simple Way to Share Privileged Information

By , August 6, 2010

Whether you’re selling retail and wholesale items, or simply need to share some content with registered users, or alternatively, content only with visitors, then this simple solution will come to the rescue! It’s so simple and easy to follow, you’ll feel the power of dynamic site content!

Content for users who are logged in

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]

Content for users who are not logged in

Alternatively, you may wish to share information to only people NOT signed in. Just paste the following code on your functions.php file:

add_shortcode( 'visitor', 'visitor_check_shortcode' );

function visitor_check_shortcode( $atts, $content = null ) {
	 if ( ( !is_user_logged_in() && !is_null( $content ) ) || is_feed() )
		return $content;
	return '';
}

Anytime you write a post/page, add this to only show content to users that are not logged in:

[visitor]
Some content for the people just browsing your site.
[/visitor]

Easy and useful!

Credits goes to Justin Tadlock for this simple and elegant solution! Thanks also to WP Recipies for sharing this helpful tip!

Leave a Reply