此代码检查用户是否是第一次登录,即在注册之后。如果是这样,我想将他重定向到自定义页面。否则,将他重定向到主页或管理页面。
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
取而代之的是空白页。更新:
好吧,我不知道发生了什么。使用过滤器 Hook ,我只有在第二次登录后才能到达预期的目的地。好吧,不是真正的第二次登录,而是第二次单击登录按钮。我是这样做的:输入凭据->登录->(错误页面)->点击返回按钮->再次输入凭据->登录->(正确页面)。奇怪的。
最佳答案
您需要像这样调整过滤器调用;
// filter name, callback, priority, accepted args
add_filter('login_redirect', 'mylogin_redirect', 10, 3);
关于wordpress - 首次登录wordpress后重定向用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4267285/