How to Remove Menus from the WordPress Admin Bar @ WordPress Help

If we recently saw how to add custom menus to the WordPress admin toolbarToday we are going to take a step forward and learn how to remove those sometimes annoying elements in our admin bar that plugins and themes insist on adding.

With the techniques that we are going to see in this guide, we are going to identify the node that is adding the menu and remove it, with a couple of methods.

To begin, you must identify the IDs of the nodes that display the administration toolbar menus.

(First of all) Locate the IDs of the nodes of the different elements

The first thing is locate the node IDof the toolbar element you want to remove, for which you have to inspect the website code to identify it.

For it use the browser inspector by right clicking on the menu item you want to remove.

Once the browser inspector opens You will see that the ID is displayed in up to 3 places.

In this example, the full ID would be this:

id="wp-admin-bar-SG_CachePress_Supercacher_Purge"

The ID of the node will be the part that follows wp-admin-bar- common in all admin bar menus.

In this example, then, the node ID of this admin bar element that we want to remove I would be SG_CachePress_Supercacher_Purge.

Remove menu from admin bar (method 1)

Once identified, we would add the following code to our customizations plugin either MU plugin:

/* Quitar menu de purgar cache de SG en barra admin */
function ayudawp_quitar_menu_sg ($wp_admin_bar) {
$wp_admin_bar->remove_node('SG_CachePress_Supercacher_Purge');
}
add_action('admin_menu_bar','ayudawp_quitar_menu_sg', 999);

Once you’ve added the code, what it does is hook our custom function into admin_bar_menu and remove the specified node.

The priority parameter 999 helps ensure that the delete function is executed after the function add-node of the plugin, because the higher the priority, the later the function is executed, as we have seen in other codes that I usually share with you.

Having said that, the only part of the code you have to modify is to replace the ID of the example node, SG_CachePress_SuperCacher_Purge by the ID that in your case you want to delete.

After customizing and adding code visit your site, reload the page to check that the toolbar menu you didn’t want is gone.

This method works on any toolbar item added using the hook admin_bar_menubut … Unfortunately, not all plugins and themes use this hook to add your custom menus.

So If this method has not worked for you, it is not your fault (nor mine)but the method simply does not work for that specific plugin/theme that we have chosen for the example.

What you have to do is try the following method…

Remove menu from admin bar (method 2)

As I mentioned, many plugins use the hook admin_bar_menu to add your custom items to the admin bar, which is the method used in the code above.

But there are many other plugins, such as SG Optimizer, UpdraftPlus and more and more, that they use a different hook, wp_before_admin_bar_renderto add your menus to the admin bar.

So If the previous method did not work for you and, despite having the ID well identifiedthe menu that bothers you is still displayed, it is because of this.

The action wp_before_admin_bar_render allows developers to modify the object $wp_admin_bar before the admin toolbar is even displayed, and that’s why they use it to add their customizations.

In this case the code would be the following:

/* Quitar menu de purgar cache d SG en barra admin */
function ayudawp_quitar_menu_sg() {
        global $wp_admin_bar;
        $wp_admin_bar->remove_menu('SG_CachePress_Supercacher_Purge');
}
add_action('wp_before_admin_bar_render', 'ayudawp_quitar_menu_sg', 999);

When you have added this other code, It works almost the same as the previous one, with these differences:

  • Here the global variable is used $wp_admin_bar (instead of passing it)
  • Use remove_menu (instead of remove_node)
  • It hooks on wp_before_admin_bar_render (instead of in admin_bar_menu)

Like before, check your site, reload the page, and see if the menu you wanted to remove is gone.

This workaround should work for admin toolbar items where the first method doesn’t work.

As before, remember to change the ID of the example node for which you want to eliminate.

With one method or another, the result we want is to get past this…

To this other…

I’m sure that with one method or another you will achieve it.

Remove default WordPress menus from the admin bar (the “meneillo”)

To finish with these techniques, you can use the first method to remove from the admin toolbar any of the standard, default, WordPress elements.

The code would be like this:

/* Quitar elementos wp de barra de admin */
function ayudawp_quitar_menus_wp_barra_admin($wp_admin_bar) {
        
        $wp_admin_bar->remove_node('wp-logo'); //Quita el logo de WP
        $wp_admin_bar->remove_node('site-name'); //Quita el nombre del sitio
        $wp_admin_bar->remove_node('comments'); //Quita los comentarios
        $wp_admin_bar->remove_node('updates'); //Quita actualizaciones
        $wp_admin_bar->remove_node('customize'); //Quita el personalizador
        $wp_admin_bar->remove_node('new-content'); //Quita + añadir
        $wp_admin_bar->remove_node('search'); //Quita el buscador 
        $wp_admin_bar->remove_node('my-account'); //Quita menu de usuario
}
add_action('admin_bar_menu', 'ayudawp_quitar_menus_wp_barra_admin', 999);

As you see, you can remove everything, simply customize the code by adding or removing the elements that you want to see or not.

Did you like this article? You can’t imagine what you’re missing in Youtube!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *