WP_Admin_Bar::add_node()publicWP 3.1.0

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.

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 element
    • rel - 'rel' attribute
    • onclick - 'onclick' attribute for the a element
    • target - how to open the link '_blank' - in a new window
    • title - title attribute
    • tabindex - order when pressing the TAB key

    Default: array()

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.
Since 6.5.0 Added the ability to pass 'menu_title' for an ARIA menu name.

WP_Admin_Bar::add_node() code WP 6.8.3

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