0

my code is

add_action('init', 'user_logged_in');
function user_logged_in(){
    if( is_user_logged_in() && is_page('login')){
    wp_redirect(home_url());
    exit;
    }
}

the page slug and name is "login"

I do not want the logged in member to access the login page.

But this code does not work in functions.php.

Can you tell me what went wrong?

  • 3
    I believe init is too early to determine is_page(). Try a later hook, like template_redirect. – Jacob Peattie Aug 1 at 10:19
  • @JacobPeattie wow it work! Thanks to you, I got to know a good hook. – Kt H Aug 1 at 10:23
1

I believe init is too early to determine is_page(). Try a later hook, like template_redirect.

function wpse_344136_user_logged_in(){
    if ( is_user_logged_in() && is_page( 'login' ) ){
        wp_redirect( home_url() );
        exit;
    }
}
add_action( 'template_redirect', 'wpse_344136_user_logged_in' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.