A Cozmoslabs Product
Documentation / Developer Knowledge Base / Redirect / Redirecting users on login based on user role

Redirecting users on login based on user role

This function is redirecting users on login based on their user role.

Make sure to:

  • change where to redirect in $url variable on line 8, 10, 12 and 14
  • change user role in if condition on line 7

login_redirect is located in wp-login.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * WordPress function for redirecting users on login based on user role
 */
 
function user_login_redirect( $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( 'aussteller' ) ) {
			$url = home_url('/account/ausstellerbereich/');
		} elseif( $user->has_cap( 'topperformer' ) ) {
			$url = home_url('/account/top-performer-bereich/');
		} else {
			$url = home_url();
		}
}
return $url;
}
add_filter( 'login_redirect', 'user_login_redirect', 10, 3 );

Profile Builder Pro

Create beautiful front-end registration and profile forms with custom fields. Setup member directories, custom redirects, cutomize user emails & more using the all in one user management plugin.

Get Profile Builder

The Ultimate Membership Bundle

Combine the power of Profile Builder with Paid Member Subscriptions to set up user registration, memberships, and recurring revenue.

Get 25% off with the bundle

Help & Support

We’re here to help you every step of the way.

Open a Support Ticket