add_admin_bar_menus
Fires immediately after WordPress creates the basic admin bar items.
It can be used to add or remove toolbar items.
Execution order:
add_admin_bar_menus- admin_bar_menu —
$wp_admin_barcan be used here without declaring it global. - wp_before_admin_bar_render
- wp_after_admin_bar_render
Use a priority from 0–99 to position a custom item correctly relative to system items.
Sometimes it is more convenient to use the similar admin_bar_menu hook, which fires afterward and passes the WP_Admin_Bar object to the callback function.
Usage
add_action( 'add_admin_bar_menus', 'wp_kama_add_admin_bar_menus_action' );
/**
* Function for `add_admin_bar_menus` action-hook.
*
* @return void
*/
function wp_kama_add_admin_bar_menus_action(){
// action...
}
Examples
#1 Usage demonstration
add_action( 'add_admin_bar_menus', 'add_toolbar_node', 80 );
function add_toolbar_node() {
global $wp_admin_bar;
$wp_admin_bar->add_node(
[
'id' => 'my-link',
'title' => 'Home',
'href' => home_url(),
]
);
}
#2 Remove basic items (links) from the toolbar
Changelog
| Since 3.1.0 | Introduced. |
Where the hook is called
add_admin_bar_menus
wp-includes/class-wp-admin-bar.php 680
do_action( 'add_admin_bar_menus' );