• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2787

Original Genesis & WooCommerce Tutorials & 6000+ Guaranteed Code

Snippets

  • Try Premium
  • Log in

Hide Posts From Logged Out Users

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.

Related Code Snippets

  • 2 Ways To Hide Content From Logged Out Users In 1 Specific Category

Reader Interactions

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags
 

Loading Comments...
 

You must be logged in to post a comment.