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 |1 Answer
Reset to default 1I 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
init
is too early to determineis_page()
. Try a later hook, liketemplate_redirect
. – Jacob Peattie Commented Aug 1, 2019 at 10:19