This question comes from a member of the WordPress Help and Share group on Facebook.
Does anybody know of an easy way to make selected posts or a post category visible only to logged-in users WITHOUT using some sort of membership plugin?
This tutorial provides the PHP code you can paste at the end of your child themes functions.php file which hides the content of single posts so they are only fully readable to logged in users.
Here’s a example of whats shown on single posts in the category named lifestyle for logged out users :
The code restricts the content to 20 words and adds a login link which redirects back to the single post the user wants to access.
The following screenshot shows the full content accessible to logged in users :
And here’s the code :
Add Login Form
You can also add a login form for existing users along with a link to the register page for new users which looks like this:
And here’s the code :
Hide Specific Single Posts
Swap out the conditional tags in the above ternary operator on the 2nd last line of code to match your own requirements.
$output = ! is_user_logged_in() && is_single(array( '1', '2', '3' ) ) ? $limit : $content;
The above code restricts access to single post i.d’s 1, 2 & 3.
Uses is_user_logged_in()
Tested using the Genesis Sample child theme by StudioPress.
Was This Tutorial Helpful?