Redirect admins to their subsite admin's dashboard

When a subsite admin logged into his site domain.com/subsite/wp-admin, he can easily access the domain.com/wp-admin, page, where this message will be shown:

https://monosnap.com/file/P73ezP8JACjljsSb2q4UsWLBH4w7YM

I would like to redirect these users, back to their wp-admin page domain.com/subsite/wp-admin right away.

Please advise if it is possible

  • Ash
    • WordPress Hacker

    Hello Graham

    Would you please try the following code?

    function go_to_primary_blog($redirect_to, $request, $user){
    $user_info = get_userdata($user->ID);
    return get_blogaddress_by_id($user_info->primary_blog) . 'wp-admin';
    }
    add_filter('login_redirect', 'go_to_primary_blog', 10, 3);

    You can use this code in your child theme’s functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Let me know if it works for you.

    Have a nice day!

    Cheers,

    Ash

  • Ash
    • WordPress Hacker

    Hello Graham

    I think I got you wrong, sorry about that. Please try the following code instead of my previous one:

    add_action( 'admin_page_access_denied', function() {
    $blogs = get_blogs_of_user( get_current_user_id() );
    $current_blog_id = get_current_blog_id();
    $user = get_userdata( get_current_user_id() );

    foreach( $blogs as $blog ) {
    if( $blog->userblog_id != $current_blog_id ) {
    wp_redirect( get_blogaddress_by_id( $user->primary_blog ) . 'wp-admin' );
    exit;
    }
    }
    }, 10 );

    You can use this code in your child theme’s functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,

    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.