WP_Admin_Bar::add_node()
Adds an element (link) to the Admin bar (top menu on the front end).
This is an alias for the method: WP_Admin_Bar::add_menu().
This method should be used on the hook admin_bar_menu.
Also read 11 hacks for the Admin bar.
Method of the class: WP_Admin_Bar{}
No Hooks.
Returns
null. Nothing.
Usage
$WP_Admin_Bar = new WP_Admin_Bar(); $WP_Admin_Bar->add_node( $args );
- $args(array) (required)
Arguments for the added element. The array may contain the following keys:
-
id(string) (required)
ID of the element.If not specified, it will be created from title with a warning "Menu ID must not be empty" if debug is enabled.
-
title(string) (required)
Title/anchor of the link. HTML tags can be used here, for example to add an icon. -
href(string)
URL of the link.If not specified, the link will not be formed and an element with text will be displayed. This may be useful when, for example, you need to create a parent item that should not lead anywhere. Or to display some information (text) as an admin bar element.
Default: false -
parent(string)
ID of the parent element to which the added element should become a child.
Default: false -
group(boolean)
Is the link group-related. Does it belong to any group.
Default: false -
meta(array)
Additional data for the link. Additional attributes for the A tag. Can be:html- any HTML code or just text that will be output immediately after the A tag.class- 'class' attribute for the li elementrel- 'rel' attributeonclick- 'onclick' attribute for the a elementtarget- how to open the link '_blank' - in a new windowtitle- title attributetabindex- order when pressing the TAB key
Default: array()
-
Examples
#1 Add a link and a child link to the admin-bar
// 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', // the id parameter from the first link
'id' => 'some_id', // your id, so you can add child links
'title' => 'Subsidiary link,
'href' => 'http://example.com/subpage',
) );
}
The result is:
To change the position of the whole block of links, item (put it before or after some block), change (int) 30 - the more, the closer to the end.
#2 Cild link to the plugins page in the site menu
// 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', // its own id, so you can add child links
'title' => 'Plugins',
'href' => admin_url('plugins.php'),
) );
}, 100 );
} #3 How to find the menu item ID
To configure a menu item, you must first find the correct link id (node).
Right-click on the menu link in your browser and select Inspect Element to open the inspector, where you can see the page html code.
Find the id tag of the selected element.
For example, if I need to set up a drop-down list of sites - I will find the id: wp-admin-bar-site-name. The ID we need is site-name, i.e. we have to remove wp-admin-bar-.
List of IDs (nodes)
wp-logo about wporg documentation support-forum feedback site-name dashboard themes customize widgets menus customize-background customize-header comments new-content new-post new-media new-page new-user edit user-actions (on the right - next to your avatar image) user-info edit-profile logout
Removing a top-level node removes the link and the submenu (if there is one).
Examples of use
The following code adds a link to the media library in the dropdown menu of the site name.
add_action( 'admin_bar_menu', 'add_link_to_admin_bar',999 );
function add_link_to_admin_bar( $admin_bar ) {
$args = array(
'parent' => 'site-name',
'id' => 'media-libray',
'title' => 'Media Library',
'href' => esc_url( admin_url( 'upload.php' ) ),
'meta' => false
);
$admin_bar->add_node( $args );
}
The following code adds a media library and a link to plugins to the dropdown menu of the site.
add_action( 'admin_bar_menu', 'add_links_to_admin_bar',999 );
function add_links_to_admin_bar( $admin_bar ) {
$args = array(
'parent' => 'site-name',
'id' => 'media-libray',
'title' => 'Media Library',
'href' => esc_url( admin_url( 'upload.php' ) ),
'meta' => false
);
$admin_bar->add_node( $args );
$args = array(
'parent' => 'site-name',
'id' => 'plugins',
'title' => 'Plugins',
'href' => esc_url( admin_url( 'plugins.php' ) ),
'meta' => false
);
$admin_bar->add_node( $args );
}
The following code adds the top-level link and submenu links of the media library and plugins to the Custom Made drop-down list.
add_action( 'admin_bar_menu', 'add_top_link_to_admin_bar',999 );
function add_top_link_to_admin_bar( $admin_bar ) {
// add a parent item
$args = array(
'id' => 'custom',
'title' => 'Custom Made',
'href' => 'http://example.com/', // Showing how to add an external link
);
$admin_bar->add_node( $args );
// add a child item to our parent item
$args = array(
'parent' => 'custom',
'id' => 'media-libray',
'title' => 'Media Library',
'href' => esc_url( admin_url( 'upload.php' ) ),
'meta' => false
);
$admin_bar->add_node( $args );
// add a child item to our parent item
$args = array(
'parent' => 'custom',
'id' => 'plugins',
'title' => 'Plugins',
'href' => esc_url( admin_url( 'plugins.php' ) ),
'meta' => false
);
$admin_bar->add_node( $args );
}
Changelog
| Since 3.1.0 | Introduced. |
| Since 4.5.0 | Added the ability to pass 'lang' and 'dir' meta data. |
| Since 6.5.0 | Added the ability to pass 'menu_title' for an ARIA menu name. |