Skip Navigation

[Resolved] Login Form Redirect Issue

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to redirect his users after they had logged in based on their roles.

Solution:

To do this you can follow the example in the link below.

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;
        } elseif ( in_array( 'client', $user->roles ) ) {
            return home_url('/wp/client/dashboard/');
        }elseif( in_array( 'legalpro', $user->roles ) ){
return   home_url('/wp/legal-professional/dashboard/');
}
    } else {
        return $redirect_to;
    }
}
 
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

Add it to your functions.php file.

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Author
Posts
#622643

We are having issues with our login form. We currently have two user roles set up: Client and Legal Professional. Each of these user roles have different access groups configured.

Both user types are using the same login form. Here is the code for the login form:

[wpv-conditional if="('[wpv-current-user info='id']' eq '')"]

Login
[wpv-login-form]

Forgot Password?
[wpv-forgot-password-form]

[/wpv-conditional]
[wpv-conditional evaluate="false" if="('[wpv-current-user info='id']' eq '')"]

You are logged in.

[/wpv-conditional]

When a user logins, the site should redirect them to the appropriate dashboard (/wp/client/dashboard) OR (/wp/legal-professional/dashboard). Here is the code being used for the redirect:

function my_login_redirect20( $url, $request, $user ){
if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {

if( $user->has_cap( 'administrator' )) {
$url = admin_url();
} elseif ( $user->has_cap( 'client' )) {
$url = home_url('/wp/client/dashboard/');
} elseif ( $user->has_cap( 'legalpro' ))  {
$url = home_url('/wp/legal-professional/dashboard/');
} else {
	$url = home_url();
}
}
return $url;
}
add_filter('login_redirect', 'my_login_redirect20', 10, 3 );

Currently, the site always redirects the user to /wp/legal-professional/dashboard, regardless of the user type. The Client user role does not have access to this page, so they are shown the 404 page.

I can’t find the setting or problem that is redirecting all users to the same URL. Any help is greatly appreciated. Thanks.

#622736

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Zach,

Thank you for contacting our support forum .

Could you try adding this

$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
    //The user has the "author" role
}

You may not need this line $user = wp_get_current_user(); but you can still try and let me know.
Thanks,
Shane

#622743
Screen Shot 2018-03-06 at 3.13.36 PM.png

Thanks Shane. That helps, but it still isn't redirecting to the correct URL. The $user = wp_get_current_user(); line seems to be necessary, because now that I have that added, every login is redirecting to the homepage (found in the else brackets). Therefore, I'm thinking the code used to specify user roles is not working correctly. I checked the slugs (see attached) and everything appears to be correct.

I wasn't sure where to add the if statement that you provided, and if I would be doing that for every user role (administrator, legal professional, & client).

Any ideas?

#622752

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Zach,

I took a look at the wordpress doc example and will be using it as a base.

First you can test this exact to see if it works.

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

Then if that works then we customize it like this.

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;
		} elseif ( in_array( 'client', $user->roles ) ) {
			return home_url('/wp/client/dashboard/');
		}elseif( in_array( 'legalpro', $user->roles ) ){
return   home_url('/wp/legal-professional/dashboard/');
}
	} else {
		return $redirect_to;
	}
}

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

Please let me know if this helps.
Thanks,
Shane

#623081

Hi Shane,

Thanks for this information. The first block of code works. As for the second block of code, the legal professional role is redirecting to the correct location, but the client user role does not redirect to the correct location. When I login as a client, I am redirected to /wp/legal-professional/dashboard when I should be redirected to /wp/client/dashboard.

#623322

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Zack,

I tested this on your site and it worked flawlessly.

Could you try again and let me know if it works for you.

Thanks,
Shane

#623330

I know it wasn't working yesterday, but looks like everything is working as it should now. Not sure why but glad it's working. Thanks for your help!!