Remove root blog from "my sites" menu in admin bar

I saw in another thread how to remove the root blog:
https://premium.wpmudev.org/forums/topic/removing-bp-root-blog-from-the-my-sites-menu-in-admin-bar

with this code:

<?php
add_action('admin_bar_menu', 'remove_root_blog');
function remove_root_blog($wp_admin_bar) {
    $blog_id = 1;

    $mysites = $wp_admin_bar->user->{'blogs'};

    unset($wp_admin_bar->user->{'blogs'});
    unset($mysites[$blog_id]);

    $blogs = array();
    foreach($mysites as $id => $info) {
        $blogs[$id] = strip_tags($info->blogname);
    }

    $blogs = array_map('strtoupper', $blogs);
    //asort($blogs);

    foreach ($blogs as $id => $title) {
        $wp_admin_bar->user->{'blogs'}->$id = $mysites[$id];
    }
}
?>

However, what if I am superadmin and want to have access to the root blog, can the code be modified so it applies ONLY to "administrators" who have sub sites but "super admins" will still see the link to the root blog?

Or is there a better method since this was posted?

thanks!

  • Luís

    Hi @jmail2,

    Hope you're doing well today!

    I made some research and I didn't find other options. I add a condition to non network admin:

    <?php
    add_action('admin_bar_menu', 'remove_root_blog');
    function remove_root_blog($wp_admin_bar) {
    
        if (! is_network_admin()) {
            $blog_id = 1;
    
           $mysites = $wp_admin_bar->user->{'blogs'};
    
            unset($wp_admin_bar->user->{'blogs'});
            unset($mysites[$blog_id]);
    
            $blogs = array();
            foreach($mysites as $id => $info) {
                $blogs[$id] = strip_tags($info->blogname);
            }
    
            $blogs = array_map('strtoupper', $blogs);
            //asort($blogs);
    
            foreach ($blogs as $id => $title) {
                $wp_admin_bar->user->{'blogs'}->$id = $mysites[$id];
            }
        }
    }
    ?>

    This code can work to you?

    I hope this information has been helpful. If I can help you in this or other questions, please let me know!

    Cheers, Luís

  • George

    Hey @Luis, thanks!
    Unfortunately that also removes the root blog link for the super-admin as well as administrators.

    So like on that other post I referenced, sometimes the root blog is in "my sites" for everyone but I have other sites where the root blog is never shown to admin of of sub-sites, what determines if the link to the root blog appears for all? Did I set something up wrong or miss a setting?

    Is it something in the reg page or do you know?

    My site that is showing the root blog link to all is using paid membership pro but I have another with a basic reg page that doesn't charge a fee and it does not add the link to all?

    thanks!!!

  • Luís

    Hi @jmail2,

    Hope you're doing well today!

    Firstly, sorry for my late reply and all the problems it may have caused.

    Try with "is_super_admin" instead of "is_network_admin":

    <?php
    add_action('admin_bar_menu', 'remove_root_blog');
    function remove_root_blog($wp_admin_bar) {
    
        if (! is_super_admin()) {
            $blog_id = 1;
    
           $mysites = $wp_admin_bar->user->{'blogs'};
    
            unset($wp_admin_bar->user->{'blogs'});
            unset($mysites[$blog_id]);
    
            $blogs = array();
            foreach($mysites as $id => $info) {
                $blogs[$id] = strip_tags($info->blogname);
            }
    
            $blogs = array_map('strtoupper', $blogs);
            //asort($blogs);
    
            foreach ($blogs as $id => $title) {
                $wp_admin_bar->user->{'blogs'}->$id = $mysites[$id];
            }
        }
    }
    ?>

    Cheers, Luís

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.