Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Guest Login Redirect on view posts

I have a wp site and I try to force guest to login/register before viewing only SINGLE POSTS.(not pages, archives etc.).

I have this function:

// Redirect users who arent logged in...
function login_redirect() {

    // Current Page
    global $pagenow;

    // Check to see if user in not logged in and not on the login page
    if(!is_user_logged_in() && $pagenow != 'wp-login.php')
          // If user is, Redirect to Login form.
          auth_redirect();
}
// add the block of code above to the WordPress template
add_action( 'wp', 'login_redirect' );

The problem with this function is that it redirects guest to login when they view any page/post/etc. Can someone help me modify this function to redirect only on viewing single posts? Thanks, Iasmina

like image 504
user3507955 Avatar asked Apr 07 '14 18:04

user3507955


1 Answers

Try this:

   function login_redirect() {
   // Current Page
   global $pagenow;

   // Check to see if user in not logged in and not on the login page
         if (!is_user_logged_in() && $pagenow != 'wp-login.php' && is_single() && !is_home()) {              
    auth_redirect();
       }
}

// add the block of code above to the WordPress template
add_action('wp', 'login_redirect');
like image 89
Nadeem Khan Avatar answered Oct 23 '22 00:10

Nadeem Khan