How to Show Your WordPress Posts Only to Registered Users?


I have just finished about a week ago a small project while also teaching WordPress to a buddy of mine, and the post privacy issue came up. As it turns out this is something that quite a number of people are looking for, however after a few hours of googleing I have to tell you, this information is not very easy to find. Not the complete info anyway, which gave me the idea to gather all the bits of sources together and post it in one place, namely, here.

What I and probably you need as well, is to make a post visible only to registered users. Unfortunately, WordPress, while extremely powerful and intuitive, this is a feature they don’t really have. But there is a rather simple workaround to it. Since I don’t want to confuse anyone. I’ll keep it as simple as it can get.

  1. Turn the specific post from public to private (post settings on the right).
  2. In the text editor, switch from “Visual” to “Text”.
  3. At the very top, before any text you might have there, paste this line: <!–?php if (current_user_can(‘subscriber’)) { ?>
  4. At the very bottom of your text, paste this line:  <!–?php } ?>

Now, this is all you need to know when it comes to the posts being accessible only by registered users, and this is what most sites and bloggers will tell you. BUT this on its own will not work. Trust me, I’m an engineer! The above mentioned 4 steps on their own will not work. In order to make this little cheat work, we need to add a few lines of php to your “functions.php” file, which you can access by going to the editor on the left and looking for the “functions.php” file in the list situated on the right.

5.   Click on “functions.php” to edit and copy to the very top the following lines:

              $subRole = get_role( ‘subscriber’ ); 

              $subRole->add_cap( ‘read_private_posts’ );

And that’s how the cookie crumbles. Obviously save or update the file. This is pretty much it.

You can do the exact same thing with pages as well. The only difference is that you’ll have to use this line: $subRole->add_cap( ‘read_private_pages’ );   instead of the one in step 5. By adding both lines you’ll block unregistered users from private pages and posts as well. Hope you find this useful.

Note: this is a WordPress.org feature. WordPress.com users do not have access to source files, therefore they cannot edit them.

Advertisements

2 responses to “How to Show Your WordPress Posts Only to Registered Users?

  1. Pingback: How to Show Your WordPress Posts Only to Registered Users – Blog | Scion Players·

  2. I find this absolutely useful. The only problem I have is that it does not work. The 4 first steps go perfectly fine. But when I add the 2 lines at the very top of my functions.php file, I get error messages and that’s it. What am I doing wrong?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s