Last active
April 14, 2020 15:37
-
-
Save wpscholar/1386782 to your computer and use it in GitHub Desktop.
WordPress function for redirecting users on login based on user role
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
`/**
*/
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 );`