Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress function for redirecting users on login based on user role
<?php
/**
* WordPress function for redirecting users on login based on user role
*/
function my_login_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('/members-only/');
}
}
return $url;
}
add_filter('login_redirect', 'my_login_redirect', 10, 3 );
@eclectic-coding
Copy link

eclectic-coding commented Sep 9, 2016

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