Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Last active April 14, 2020 15:37
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save wpscholar/1386782 to your computer and use it in GitHub Desktop.
Save wpscholar/1386782 to your computer and use it in GitHub Desktop.
WordPress function for redirecting users on login based on user role
/**
* WordPress function for redirecting users on login based on user role
*/
function login_redirect( $url, $request, $user ){
// Is there a user to check?
if( !is_wp_error($user) && is_array($user->roles) ){
// Check for admins
if( in_array('administrator', $user->roles) )
return admin_url();
else
return $url . '?var=test';
}
}
add_filter('login_redirect', array($this, 'login_redirect'), 10, 3 );
@eclectic-coding
Copy link

Thanks... worked beautifully. In my case I wanted subscribers to be redirected the the home_url so I added another function for that that works nicely.
`/**

  • WordPress function for redirecting users on login based on user role
    */
    function my_logout_redirect( $url, $request, $user ){
    if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
    if( $user->has_cap( 'administrator' ) ) {
    $url = admin_url();
    } else {
    $url = home_url('/');
    }
    }
    return $url;
    }

add_filter('logout_redirect', 'my_logout_redirect', 10, 3 );`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment