login - add_action('init') not work

my code is add_action('init', 'user_logged_in');function user_logged_in(){if( is_user_logged_in() &

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?

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?

Share Improve this question asked Aug 1, 2019 at 10:12 Kt HKt H 152 silver badges6 bronze badges 2
  • 3 I believe init is too early to determine is_page(). Try a later hook, like template_redirect. – Jacob Peattie 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
Add a comment  | 

1 Answer 1

Reset to default 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' );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745276751a4620088.html

相关推荐

  • login - add_action('init') not work

    my code is add_action('init', 'user_logged_in');function user_logged_in(){if( is_user_logged_in() &

    3小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信