metinsaylan // rogue developer

How to hide members only content in your posts

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 ?&gt; : [php title=”functions.php”]function shailan_user_only_shortcode( $atts, $content = null ){ if( null != $content && current_user_can(‘read’) ){ return $content; } else { return ‘< MEMBERS ONLY SECTION >’; } }

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.

**Preview: **

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

Hope you like it.

Search How to hide members only content in your posts on Google