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
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With