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