You can use the login_redirect filter to control the redirect location. It’s a WordPress core filter that User Switching respects when switching between users.
Here’s some completely untested code which should get you some way toward what you want to do:
add_action( 'switch_back_user', function( $user_id, $old_user_id ) {
add_filter( 'login_redirect', function( $redirect_to, $requested_redirect_to, $new_user ) {
if ( user_can( $new_user->ID, 'administrator' ) ) {
return admin_url();
} else {
return $redirect_to;
}
}, 10, 3 );
}, 10, 2 );
Also, if your custom URLs show up blank for administrators then you should probably look at adding something a little more meaningful to them, so they aren’t blank 🙂
That worked!!!! Look at that, you said completely untested and because you are the pro you are, it didn’t even need to be tested, it worked the first time around! Thank you very much for your quick response and solution. You are awesome and I love this plugin!
Hi John,
I guess this filter is for a specific user.
What about we need to use it for a role?
I mean what should we use when we need to manage it to switch back to the dashboard for a specific role?
Thank you.
It is working for all Roles and it worked for me (switch back to users list):
add_action( ‘switch_back_user’, ‘switch_back_user_redirect’ );
function switch_back_user_redirect()
{
wp_redirect( home_url( ‘wp-admin/users.php’ ) );
exit();
}
Thank you.