简体   繁体   中英

WordPress - Identify user type for first time login redirect

We're using Peters Redirect plugin and looking to extend it to where it identifies user type and based on that it redirects the user (on first login) to the respective TOS content. The 'first time login redirect' works, and I'll paste that below. We just need some advice for adding the user identification part.

Thanks in advance for the help!

// 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 );

You can do something like

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';    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM