Re-Direct ALL Users to the Home Page IF not logged in

Question

I just read the codex on wp_redirect(); and found an example that I modified. Problem is, all I get is an error in my browser saying "The page isn’t redirecting properly".

I need all users, unless logged in, to be re-directed to the home page (home_url()).

Can someone please help me?

add_action( 'template_redirect', 'not_logged_in_redirect_home' );
add_action( 'do_feed', 'not_logged_in_redirect_home' );
function not_logged_in_redirect_home() {

        if ( is_home() ) return;

        if ( ! is_user_logged_in() && is_home() ) return;

        if ( ! is_user_logged_in() && !is_home() ) {
    
            wp_redirect( home_url() );
    
            exit;
    }
}
0
Harold Aldersen 2 years 2020-12-29T13:10:44-05:00 0 Answers 3 views 0

Leave an answer

Browse
Browse