识别用户类型为第一次登录重定向


WordPress - Identify user type for first time login redirect

我们正在使用Peters重定向插件,并希望将其扩展到识别用户类型的地方,并基于此重定向用户(在第一次登录时)到各自的TOS内容。"第一次登录重定向"工作,我将粘贴在下面。我们只需要一些关于添加用户标识部分的建议。

提前感谢您的帮助!

// Send new users to a special page
function redirectOnFirstLogin( $custom_redirect_to, $redirect_to, $requested_redirect_to, $user )
{
// URL to redirect to
$redirect_url = 'http://url.com/firsttime';
// How many times to redirect the user
$num_redirects = 1;
// If implementing this on an existing site, this is here so that existing users don't suddenly get the "first login" treatment
// On a new site, you might remove this setting and the associated check
// Alternative approach: run a script to assign the "already redirected" property to all existing users
// Alternative approach: use a date-based check so that all registered users before a certain date are ignored
// 172800 seconds = 48 hours
$message_period = 172800;
$key_name = 'redirect_on_first_login';
// Third parameter ensures that the result is a string
$current_redirect_value = get_user_meta( $user->ID, $key_name, true );
if( strtotime( $user->user_registered ) > ( time() - $message_period )
    && ( '' == $current_redirect_value || intval( $current_redirect_value ) < $num_redirects )
  )
{
    if( '' != $current_redirect_value )
    {
        $num_redirects = intval( $current_redirect_value ) + 1;
    }
    update_user_meta( $user->ID, $key_name, $num_redirects );
    return $redirect_url;
}
else
{
    return $custom_redirect_to;
    }
 }
 add_filter( 'rul_before_user', 'redirectOnFirstLogin', 10, 4 );

你可以这样做

if(current_user_can('subscriber'))
{
    $redirect_url = 'http://url.com/subscriberTOS';    
}
else if(current_user_can('author'))
{
    $redirect_url = 'http://url.com/authorTOS';    
}
else if(current_user_can('contributor'))
{
    $redirect_url = 'http://url.com/contributorTOS';    
}
else if(current_user_can('editor'))
{
    $redirect_url = 'http://url.com/editorTOS';    
}
else if(current_user_can('administrator'))
{
    $redirect_url = 'http://url.com/administratorTOS';    
}