WP-Mix

A fresh mix of code snippets and tutorials

Members-only content via shortcode

Here is a sweet little WordPress shortcode to display content only to logged-in users. Visitors not logged in will see a custom message saying whatever you’d prefer.

To implement, just add the following code to your theme’s functions.php file:

function member_check_shortcode($atts, $content = null) {
	if (is_user_logged_in() && !is_null($content) && !is_feed()) {
		return do_shortcode($content);
	}
	return '<p>Please register to add content.</p>';
}
add_shortcode('member', 'member_check_shortcode');

Then you can use it to display content to logged-in users only, like so:

[member]Welcome logged-in user..[/member]

Another example, displaying the content of another shortcode to members only:

[member][user-submitted-posts][/member]

Thanks to LapanWebsite for posting this tip on the forums for User Submitted Posts.

Related Posts

Learn more

WordPress Themes In Depth