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?

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

1 Answer 1

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 and acknowledge you have read our privacy policy.

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