Send Users to Their Own Site After Login
When someone logs in on a site where they are not a member, sends them to the dashboard of their primary site instead of an error page.
Opens WPIDE on
Code
if ( ! is_multisite() ) {
return;
}
add_filter( 'login_redirect', function ( $redirect_to, $requested, $user ) {
if ( ! $user instanceof WP_User || is_super_admin( $user->ID ) || is_user_member_of_blog( $user->ID ) ) {
return $redirect_to;
}
$primary = get_active_blog_for_user( $user->ID );
return $primary ? get_admin_url( $primary->blog_id ) : $redirect_to;
}, 10, 3 );
Notes
Users who belong to the site they log in on are redirected as usual.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.