此代码检查用户是否第一次登录,即注册后。如果是的话,我想将他重定向到自定义页面。否则,将其重定向到主页或管理页面。
function mylogin_redirect() {
global $user_ID;
if( $user_ID ) {
$user_info = get_userdata( $user_ID );
// If user_registered date/time is less than 48hrs from now
// Message will show for 48hrs after registration
if ( strtotime( $user_info->user_registered ) > ( time() - 172800 ) ) {
header("Location: http://example.com/custompage");
} elseif( current_user_can( 'manage_options' )) {
header("Location: http://example.com/wp-admin/");
} else {
header("Location: http://example.com/");
}
}
}
add_action('wp_head', 'mylogin_redirect');
但它不起作用?我的猜测是它没有被迷上wp_head ...... 我使用login_redirect过滤器尝试了以下内容:
function mylogin_redirect($redirect_to, $url_redirect_to = '', $user = null) {
global $user_ID;
if( $user_ID ) {
$user_info = get_userdata( $user_ID );
// If user_registered date/time is less than 48hrs from now
// Message will show for 48hrs after registration
if ( strtotime( $user_info->user_registered ) > ( time() - 172800 ) ) {
return get_bloginfo('url') . "/custompage/";
} elseif( current_user_can( 'manage_options' )) {
return admin_url();
} else {
return get_bloginfo('url');
}
}
}
add_filter('login_redirect', 'mylogin_redirect');
虽然它让我登录,但它并没有让我到任何地方,只有http://example.com/wp-login.php
而不是空白页面。
更新 好的,我不知道发生了什么。使用过滤器挂钩,我只能在第二次登录后才能到达目的地。好吧,不是第二次登录,而是第二次点击登录按钮。我是这样做的:输入凭证 - >登录 - > (错页) - >点击按钮 - >再次输入凭据 - >登录 - > (正确的页面)。怪异。
答案 0 :(得分:4)
您需要像这样调整过滤器调用;
// filter name, callback, priority, accepted args
add_filter('login_redirect', 'mylogin_redirect', 10, 3);
答案 1 :(得分:2)
Redirecting users on first login in WordPress:基于Cookie的解决方案&基于用户元表的解决方案