I’ve tried this to redirect people who aren’t logged in to certain page. It ultimately shouldn’t matter what front end page they visit, but should redirect to whatever page is chosen as the landing page (which I’m assuming would be the URL in the wp_redirect). I still need access to wp-login and dashboard etc…

I placed the following code in my functions.php, but did not work.

function my_redirect() {    
   if ( $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] == 'mybigfatsite.com/' ) {
    if ( ! is_user_logged_in() ) {
       wp_redirect( 'mybigfatsite.com/landing/' );
       exit;
    }
}
}
add_action( 'init', 'my_redirect' );

Thanks for any help!

1 Answer
1

The is_login_page() function is taken from here

function is_login_page() {
    if ( $GLOBALS['pagenow'] === 'wp-login.php' && ! empty( $_REQUEST['action'] ) && $_REQUEST['action'] === 'register' )
        return true;
    return false;
}

function my_redirect() {  
    //if you have the page id of landing. I would tell you to use if( is_page('page id here') instead
    //Don't redirect if user is logged in or user is trying to sign up or sign in
    if( !is_login_page() && !is_admin() && !is_user_logged_in()){
        //$page_id is the page id of landing page
        if( !is_page($page_id) ){
            wp_redirect( get_permalink($page_id) );
            exit;
        }
    }
}
add_action( 'template_redirect', 'my_redirect' );

Leave a Reply

Your email address will not be published. Required fields are marked *