WP_Admin_Bar::add_node()publicWP 3.1.0

Adds a node to the menu.

Method of the class: WP_Admin_Bar{}

No Hooks.

Return

null. Nothing.

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'.
    Default: ''

Examples

0

#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.

0

#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 );
}
0

#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.

WP_Admin_Bar::add_node() code WP 6.1.1

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 );
}