wordpress-icon-150x150During WordPress theme, or plugin development there are times when you want to show certain page’s content based upon the user. For instance, you want to display a download link to only registered users and display a Register link to un-registered or new users. So we are going to discuss a way through which you can simply wrap this content in a shortcode.


Using Shortcode allows you to display this kind of special content anywhere on page or post. This is very simple to achieve, just  follow the below given steps to display the content to registered users only –

1) Connect to FTP and go to themes folder. Alternatively you can go to WordPress Admin dashboard and click on Appearance > Editor

2) Open functions.php file.

3) Copy the following code at the end of file, make sure it’s inside PHP tags ( )

 * Register a shortcode with Wordpress Current theme
add_shortcode('members_content', 'members_content_check');
function members_content_check($atts, $content = null) {
     * check if user is logged in, and content is not null or is not feed
     * if all conditions meet, then the content which is wrapped between shortcodes
     * is returned and is displayed on the webpage
    if (is_user_logged_in() && !is_null($content) && !is_feed())
        return $content;
     * Else we return a sorry message or you can even return an empty string
    return 'Sorry ! Only registered members can view this content.';

4) Once you are done adding the code, save the file. That’s all.

Now let’s see how to use it in the editor while creating posts/pages. Since shortcodes are very simple to use, hence wrap the content inside [members_content] … [/members_content] tag as show below.

Special content for special registered members

That’s all ! You can add any length of content that you want to display to members only. This is a small nifty trick, but very useful. Hope it helps you as well.

Stay Digified !!
Sachin Khosla

Share this post: