Require login to view posts

Adding this PHP code to the functions.php of your wordpress theme will require users to login before viewing specific posts defined by and array of post IDs. Once the user is logged in they will be redirected to the original post.

 

 

// add to functions.php
function you_must_login() {
global $post;
if ( !is_single() ) // Use: !is_single() for members only posts and, Use:  !is_page() for members only pages.
return;
$post_ids = array( 188, 185, 171 ); // array of post IDs that force users to login before reading
if ( in_array( (int) $post->ID, $post_ids ) && !is_user_logged_in() ) {
auth_redirect();
}
}

Leave a comment