WP_Admin_Bar::add_node()
Adds a node to the menu.
Method of the class: WP_Admin_Bar{}
No Hooks.
Return
null
. Nothing (null).
Usage
$WP_Admin_Bar = new WP_Admin_Bar(); $WP_Admin_Bar->add_node( $args );
- $args(array) (required)
Arguments for adding a node.
-
id(string)
ID of the item. -
title(string)
Title of the node. -
parent(string)
Optional. ID of the parent node. -
href(string)
Optional. Link for the item. -
group(true|false)
Optional. Whether or not the node is a group.
Default: false - meta(array)
Meta data including the following keys: 'html', 'class', 'rel', 'lang', 'dir', 'onclick', 'target', 'title', 'tabindex', 'menu_title'.
Default: ''
-
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. |