Adding elements (links) to the admin bar (toolbar)
To add links, use the WP_Admin_Bar::add_menu() method, also known as WP_Admin_Bar::add_node(). The addition should be done on the admin_bar_menu hook.
You can add both top-level links and child links. For example, let's add a link and a child link to the toolbar:
// Adds a link to the admin bar
add_action( 'admin_bar_menu', 'my_admin_bar_menu', 30 );
function my_admin_bar_menu( $wp_admin_bar ) {
$wp_admin_bar->add_menu( array(
'id' => 'menu_id',
'title' => 'External Link',
'href' => 'http://example.com',
) );
// child link
$wp_admin_bar->add_menu( array(
'parent' => 'menu_id', // id parameter from the first link
'id' => 'some_id', // your id to be able to add child links
'title' => 'Child Link',
'href' => 'http://example.com/subpage',
) );
}
As a result, we will get:
To change the position of the entire block of links, (put it before or after a certain block), change the number 30 — the larger the number, the closer to the end.
Another example: a child link in the site menu to the plugins page
// child link in the site menu to the plugins page
if( ! is_admin() ){
add_action( 'admin_bar_menu', function ( $wp_admin_bar ) {
$wp_admin_bar->add_menu( array(
'parent' => 'site-name', // id of the parent element
'id' => 'plugins_link', // your id to be able to add child links
'title' => 'Plugins',
'href' => admin_url('plugins.php'),
) );
}, 100 );
}
See the list of all possible parameters in WP_Admin_Bar::add_menu().
—
Note embeded into: 11 Hacks for the WordPress Admin Bar (Toolbar)
