There are some great options if you need a robust solution when deciding to add a member’s only section to your WordPress site or if you want to create a pay-wall for some of your content. Two solid solutions are WishList Member (pay) and Justin Tadlock’s Members Plugin (free). But what if you want to use the simple permissions already built into a normal WordPress installation?
WordPress already gives you the option of whether or not you want to allow your readers to create an account. Because of this option, you already have a “wall” that can be put in place for those people who are logged in and those visitors who are not logged in. And with only 7 lines of code, we can create a simple way of protecting content to only people who are logged in. So our goal in this tutorial is to create a simple way for display certain pieces of content to only “registered users” or those visitors who are logged in.
First, we need to create a shortcode. WordPress defines shortcode as “a WordPress-specific code that lets you do nifty things with very little effort.” We will create a shortcode by adding the following code to the functions.php file that resides in your theme folder. (**See note at bottom for creating a functions.php file)
function checkreg_shortcode($atts, $content = null) {
if (is_user_logged_in() && !is_null($content) && !is_feed()) {
return $content;
} else {
return ‘We are sorry, but this part of the post is only available to our logged in members. Click here to become a member!‘;
}
}
add_shortcode(‘regmember‘, ‘checkreg_shortcode‘);
Now, once we have the above code in our functions.php file (and we have saved the file), we can go to our posts. And in any of the posts you can add the following shortcode:
[regmember]
Woohoo, you are logged and and will be able to see this text.
[/regmember]
Anything inside the [regmember] shortcode tag will now only be displayed to visitors to your site who are logged in.
The Mystery Revealed…
So, what is happening in the above code is that we have created a new function. That function is called checkreg_shortcode. This function asks WordPress to check and see whether the current visitor who is at your page/site is logged in or not. If WordPress sees that the visitor is logged in then the text that is sandwiched between the two [regmember] TEXT[/regmember] shortcode is displayed. If the visitor is not logged in, WordPress will display the apology message that appears on line 5 of the functions.php code.
Now to finish this up you may need to style the “alert” message and input the proper links for people to register for your site, because right now the above code just spits out the plain text: (We are sorry, but this part of the post is only available to our logged in members. Click here to become a member!) But what we have done in this tutorial is create 7 lines of code that effectively provide a basic form of “membership-only” functionality.
Note regarding creating a functions.php file: If you don’t have a functions.php file in your theme folder, you can create one. The only thing to remember is that if you create a functions.php file you need to have the first line of the file contain the following code:
<?php
And for the functions.php file you do not close the php bracket at the end of the file
Do you want to learn more?
WebDesign.com has an upcoming WP Developer Certification course (with only a few seats left) that teaches developers using WordPress by diving deep into the foundational requirements for being a developer and the essentials for creating successful projects for the web using WP as the CMS (Content Management System). We invite you to learn more about becoming a WebDesign.com member or the participating in the WebDesign.com Certification classes.






Nice code. It looks like the “hide” function in VBB forums. Thanks for sharing.
I am looking for this simple plugin to add to my site.
I don’t want to write the code myself
Has anyone written this plugin and what is it called.
I just want to hide content with a shortcode
i.e.
[regmember]
stuff
[/regmember]
I don’t want to install wishlist, I have that on other sites.
I just want something very very simple.
Know where I can find that?