In wordpress there are many ways to hide your members only content. You can either install a plugin to hide your posts, or you can create password protected posts that only members can access. But there is another way; Shortcodes. Here i will explain you how to hide members only content in an easy way..

In this step-by-step tutorial we will create shortcode [membersonly]. This shortcode will return content if user is logged in, and a warning for anonymous users. To enable this shortcode we will create it’s function first.

1. Open functions.php file in your theme directory.
2. Insert the following code near the bottom of file before php closing tag ?> :
[php title=”functions.php”]function shailan_user_only_shortcode( $atts, $content = null ){
if( null != $content && current_user_can(‘read’) ){
return $content;
} else {

This function uses current_user_can wordpress function to test user’s capabilities. read capability is assigned to subscribers. If you want to raise access level to editors, authors or admins you can simply change the capability to another one.
Also the message can be modified to give users more definitive and/or stylish messages.

3rd and last step is adding our shortcode using add_shortcode :
[php light=”true” title=””]add_shortcode(‘membersonly’, ‘shailan_user_only_shortcode’);

Don’t forget to save your file after edit.
You can easily hide your site content from non-subscribers using following syntax:


   Member only content wrapped here.
[/ membersonly]

This is visible to everyone.


Now you can create members-only sections in your wordpress posts using power of shortcodes.

Hope you like it.