Removing Basic Elements (Links) from the Toolbar
I've seen a solution to this task on the internet - it's not good because the items are removed after they have been added to the panel. It would be preferable not to add them there at all. This is done as follows:
/** * Removing Basic Elements (Links) from the Toolbar. */ add_action( 'add_admin_bar_menus', 'kama_remove_default_wp_menu_items', 0 ); function kama_remove_default_wp_menu_items() { /** * For full available list: * @see WP_Admin_Bar::add_menus() * @see https://wp-kama.com/note/remove-basic-items-from-toolbar */ $remove_wp_menus_items = [ /** WordPress logo (with links). {@see wp_admin_bar_wp_menu()} */ 'wp_admin_bar_wp_menu', /** Customizer. {@see wp_admin_bar_customize_menu()} */ 'wp_admin_bar_customize_menu', /** Comments. {@see wp_admin_bar_comments_menu()} */ 'wp_admin_bar_comments_menu', /** The additional group on the right (search and account). {@see wp_admin_bar_add_secondary_groups()} */ 'wp_admin_bar_add_secondary_groups', ]; foreach( $remove_wp_menus_items as $callback ){ $priority = has_action( 'admin_bar_menu', $callback ); if( $priority !== false ){ remove_action( 'admin_bar_menu', $callback, $priority ); } } }
All available WP menu items to remove (for the code above).
$remove_wp_menus_items = [ /// User-related, aligned right. /** Internal profile menu links. {@see wp_admin_bar_my_account_menu()} */ 'wp_admin_bar_my_account_menu', /** Entire profile menu. {@see wp_admin_bar_my_account_item()} */ 'wp_admin_bar_my_account_item', /** Search. {@see wp_admin_bar_search_menu()} */ 'wp_admin_bar_search_menu', /// Site-related /** WordPress logo (with links). {@see wp_admin_bar_wp_menu()} */ 'wp_admin_bar_wp_menu', /** My sites. {@see wp_admin_bar_my_sites_menu()} */ 'wp_admin_bar_my_sites_menu', /** Sites. {@see wp_admin_bar_site_menu()} */ 'wp_admin_bar_site_menu', /** Edit site. {@see wp_admin_bar_edit_site_menu()} */ 'wp_admin_bar_edit_site_menu', /** Customize theme. {@see wp_admin_bar_customize_menu()} */ 'wp_admin_bar_customize_menu', /** Updates. {@see wp_admin_bar_updates_menu()} */ 'wp_admin_bar_updates_menu', /// Content related. /** Comments. {@see wp_admin_bar_comments_menu()} */ 'wp_admin_bar_comments_menu', /** Add post, page, media, etc.. {@see wp_admin_bar_new_content_menu()} */ 'wp_admin_bar_new_content_menu', /** Edit. {@see wp_admin_bar_edit_menu()} */ 'wp_admin_bar_edit_menu', /// Others. /** The additional group (search and account) on the right. {@see wp_admin_bar_add_secondary_groups()} */ 'wp_admin_bar_add_secondary_groups', ];
Copy elements from this array to the code above to remove menu items required for your current need.
—
This Note embeded into: 11 Hacks for the WordPress Admin Bar (Toolbar)