Adds a node to the menu.
Parameters
$args
arrayrequired- Arguments for adding a node.
id
stringID of the item.title
stringTitle of the node.parent
stringOptional. ID of the parent node.href
stringOptional. Link for the item.group
boolOptional. Whether or not the node is a group. Default false.meta
arrayMeta data including the following keys:'html'
,'class'
,'rel'
,'lang'
,'dir'
,'onclick'
,'target'
,'title'
,'tabindex'
,'menu_title'
. Default empty.
Source
public function add_node( $args ) {
// Shim for old method signature: add_node( $parent_id, $menu_obj, $args ).
if ( func_num_args() >= 3 && is_string( $args ) ) {
$args = array_merge( array( 'parent' => $args ), func_get_arg( 2 ) );
}
if ( is_object( $args ) ) {
$args = get_object_vars( $args );
}
// Ensure we have a valid title.
if ( empty( $args['id'] ) ) {
if ( empty( $args['title'] ) ) {
return;
}
_doing_it_wrong( __METHOD__, __( 'The menu ID should not be empty.' ), '3.3.0' );
// Deprecated: Generate an ID from the title.
$args['id'] = esc_attr( sanitize_title( trim( $args['title'] ) ) );
}
$defaults = array(
'id' => false,
'title' => false,
'parent' => false,
'href' => false,
'group' => false,
'meta' => array(),
);
// If the node already exists, keep any data that isn't provided.
$maybe_defaults = $this->get_node( $args['id'] );
if ( $maybe_defaults ) {
$defaults = get_object_vars( $maybe_defaults );
}
// Do the same for 'meta' items.
if ( ! empty( $defaults['meta'] ) && ! empty( $args['meta'] ) ) {
$args['meta'] = wp_parse_args( $args['meta'], $defaults['meta'] );
}
$args = wp_parse_args( $args, $defaults );
$back_compat_parents = array(
'my-account-with-avatar' => array( 'my-account', '3.3' ),
'my-blogs' => array( 'my-sites', '3.3' ),
);
if ( isset( $back_compat_parents[ $args['parent'] ] ) ) {
list( $new_parent, $version ) = $back_compat_parents[ $args['parent'] ];
_deprecated_argument( __METHOD__, $version, sprintf( 'Use <code>%s</code> as the parent for the <code>%s</code> admin bar node instead of <code>%s</code>.', $new_parent, $args['id'], $args['parent'] ) );
$args['parent'] = $new_parent;
}
$this->_set_node( $args );
}
Inspect element to find the id
To adjust a menu item one has to first find the correct id (node) of the link.
Right click the menu link in your browser and select Inspect Element (name will vary from browser to browser) to open the panel to where one can see the html and css of the page you are on.
Notice the id tag.
I am looking at adjusting the sites drop down. So I will find the id: wp-admin-bar-site-name.
To find the correct node one has to remove: wp-admin-bar-. What is left is the site-name.
Nodes
The various top level and submenu nodes in the admin toolbar are:
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 (to the right – next to your avatar image)
user-info
edit-profile
logout
Removing a top level node removes the link and submenu if it has one.
The Code
If you’re using the code in the beginning of your functions.php file then include the opening php tag before the below code.
The following code adds a media library link to the site name drop down menu.
The following code adds a media library and a plugins link to the site name drop down menu.
The following code adds top level link and media library and plugins submenu links to the Custom Made drop down.
Add a Page to the Toolbar
This example will add a Page with a “my-toolbar-page” class to the Toolbar. It will be a top level item because the ‘parent’ argument is not set (it has no parent node). Put this in your theme’s functions.php file.
function replace_howdy( $wp_admin_bar ) { $my_account = $wp_admin_bar->get_node( ‘my-account’ ); $greeting = str_replace( ‘Howdy,’, ‘Hi,’, $my_account->title ); $wp_admin_bar->add_node( array( ‘id’ => ‘my-account’, ‘title’ => $greeting, ) ); } add_filter( ‘admin_bar_menu’, ‘replace_howdy’, 25 );
Make an Existing Child Node a Parent Node
This example will change the properties of an existing node by using the ID of that node. See Finding Toolbar Node ID’s on how to find existing node ID’s. The following code will make the child node with ID “new-post” (New > Post) a parent node.
Sorting The Links on the Admin Menu Bar
Use further array functions in PHP to sort the admin menu items with the following code.
Problem: Since the basic code for admin bar links is to create a separate $args variable for each array, sorting the links could be a lot of work cutting and pasting code around when you need to sort the links.
Solution: The following code does the following: 1. adds each $args into a separate element of the same $args by using PHP’s array_push() function 2. Each $args element is a multi-dimensional array that can be sorted 3. Use PHP’s sort function to sort them. 4. Traverse a for loop.
Single function to add parent/child sub menu items
This is how to add a parent or sub menu item with one function.
We can add below code in function to hide menu, if user is not super admin OR admin bar is not showing.