Custom login redirect to referring and custom URL based on role.

Hey,

I'm wondering if anyone might be able to help me out or point me in the right direction.

I've been trying for a few days here to come up with something that would redirect users upon login based on roles. Basically, I'd like to redirect everyone that is not an administrator to the referring URL and administrator to a custom URL.

The following code, thanks to someone over at WP.stackexchange.com, works well for redirecting all users after they login.

add_filter( 'login_redirect', function ( $redirect_to, $requested_redirect_to ) {
    if ( ! $requested_redirect_to ) {
        $redirect_to = wp_get_referer();
    }

    return $redirect_to;
}, 10, 2 );

but what I'd like is IF administrator, redirect to custom URL, else, to the referring URL.

I also found this code, but can't seem to adapt it to the working code above. In fact, even on its own, it doesn't seem to work.

/**
 * Redirect user after successful login.
 *
 * param string $redirect_to URL to redirect to.
 * param string $request URL the user is coming from.
 * param object $user Logged user's data.
 * @return string
 */
function my_login_redirect( $redirect_to, $request, $user ) {
	//is there a user to check?
	if ( isset( $user->roles ) && is_array( $user->roles ) ) {
		//check for admins
		if ( in_array( 'administrator', $user->roles ) ) {
			// redirect them to the default place
			return $redirect_to;
		} else {
			return home_url();
		}
	} else {
		return $redirect_to;
	}
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

I've tried numerous other plugins and snippets, have read everything here, and still, I can't seem to get it right. If anyone might be able to lend a hand or point me to another thread I may have missed, that would be great.

Thank you!
Shayne

  • Nastia

    Hello Shayne , I hope you are doing well today!

    I'm not a code expert, so I asked one of our developers to have a look. He provided the code bellow:

    function wpmudev_login_redirect_by_role( $redirect_to, $request, $user ) {
    
        if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    
            $roles_redirects = array(
                'administrator' => home_url() . '/wp-admin',
                'subscriber'    => home_url() . '/account',
                'author'        => home_url() . '/instructions'
            );
    
            foreach( $roles_redirects as $role => $redirect ){
    
                if( in_array( $role, $user->roles ) ) return $redirect; 
    
            }
    
            return home_url();
    
        } else {
            return $redirect_to;
        }
    }
    
    add_filter( 'login_redirect', 'wpmudev_login_redirect_by_role', 10, 3 );

    You can set which role should redirect where in this array, just replace the slug with your own:

    $roles_redirects = array(
                'administrator' => home_url() . '/wp-admin',
                'subscriber'    => home_url() . '/account',
                'author'        => home_url() . '/instructions'
            );

    This way depends on a user role the user will be redirected to a page you will set inside the code.

    I hope this will help!

    Cheers,
    Nastia

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.