• Hello,

    I am using the following script to redirect any users not logged into my site to the login page.

    if (
    !in_array($GLOBALS[‘pagenow’], array(‘wp-login.php’, ‘wp-register.php’))
    && !is_admin()
    && !is_user_logged_in()
    ) {
    wp_redirect(‘https://mydomain.net/wp-login.php’, 301);
    exit;
    }

    However, there are a few pages that I want non logged in users to be able to view such as my landing page, shopping cart page etc. What code can I add to this script to permit certain page exceptions.

    Thanks,
    Matt

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]

    • This topic was modified 7 years, 3 months ago by bdbrown.
    • This topic was modified 7 years, 3 months ago by bdbrown.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi Matt,

    You can use something like this

    
    add_action('wp','test_fun');
    function test_fun(){
    if (!in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php')) && !is_admin() && !is_user_logged_in()) {
     if(is_page(155) || is_page(200)){
     	
     }else{
    auth_redirect();
     }
    
     }
    }
    

    Replace is_page function parameter with your page id of shopping cart or landing pages and this will allow those pages to non-logged in users. Let me know if it works for you!

    Thread Starter mattbeeps

    (@mattbeeps)

    That worked great. Thank you very much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Redirect If Not Logged In’ is closed to new replies.