In WordPress, it’s possible to hide menu items added by plugins in the admin bar using custom code. Here are a few methods to achieve this effectively:
Method 1: Remove Menu Using wp_before_admin_bar_render
Hook
You can add the following code to your current theme’s functions.php
file or use a site-specific plugin:
function remove_plugin_admin_bar_menu() {
global $wp_admin_bar;
// Check and remove the specified menu item (by menu item ID)
// For example: Remove a menu item added by a plugin
$wp_admin_bar->remove_menu('menu_id'); // Replace 'menu_id' with the ID of the menu added by the plugin
}
add_action('wp_before_admin_bar_render', 'remove_plugin_admin_bar_menu', 999);
Method 2: Disable Menu Using admin_bar_menu
Hook
If you know which hook the specific plugin uses to add the menu, you can disable it like this:
function disable_plugin_admin_bar_menu() {
// Assuming the plugin uses the 'admin_bar_menu' hook
remove_action('admin_bar_menu', 'function_name_that_adds_menu', 999); // Replace 'function_name_that_adds_menu' with the actual function name
}
add_action('init', 'disable_plugin_admin_bar_menu');
Method 3: Use CSS to Hide
If the first method doesn’t work for you, or if you’re unsure which hook the plugin uses, you can opt for a quick workaround by adding CSS to hide the menu items.
function hide_admin_bar_menus_with_css() {
if ( ! is_admin_bar_showing() ) {
return;
}
?>
<style>
/* Hide WP Mail SMTP Admin Bar menu */
#wp-admin-bar-wp-mail-smtp-menu { display: none !important; }
/* Add more menu items to hide as needed */
#wp-admin-bar-perfmatters, #wp-admin-bar-wpforms-menu { display: none !important; }
</style>
<?php
}
add_action('wp_head', 'hide_admin_bar_menus_with_css');
add_action('admin_head', 'hide_admin_bar_menus_with_css');
You can place the above code in your theme’s functions.php
file or add it via code snippets to achieve the desired result.
How to Find Menu Item IDs or Plugin Function Names
Menu Item IDs: Open your browser’s developer tools and inspect the admin bar to find the ID of the target menu item. Use this ID in the
remove_menu()
function.Plugin Function Names: If the plugin’s code is publicly available, check the plugin’s source code to find the function name responsible for adding the menu to the admin bar.
By using these methods, you can effectively hide menu items added by plugins in the admin bar. If you have other plugins or specific requirements, feel free to customize the code further.