This function is a method of the $wp_admin_bar global object (see: WP_Admin_Bar), and can be used to add a new item to the Toolbar. You can also use it to change the properties of an item that is already in the Toolbar.

The Toolbar replaces the Admin Bar since WordPress Version 3.3.

Toolbar items are also called "nodes". Nodes can be parents for other nodes, which creates dropdown menus. Group nodes together with add_group() to create distinct sections in a Toolbar menu.

Note: This function is a method of the WP_Admin_Bar class and $wp_admin_bar global object, which may not exist except during the 'admin_bar_menu' or 'wp_before_admin_bar_render' hooks.

Note: add_menu is an alias for this method.

New as of Wednesday, June 17, 2015, see code for sorting the menu items on the admin bar.

add_node() 描述


<?php $wp_admin_bar->add_node$args ); ?>

add_node() 用法



(array) (必填) An array of arguments.

默认值: None



(string) (必填) The ID of the node.

默认值: false


(string) (可选) The text that will be visible in the Toolbar. Including html tags is allowed.

默认值: false


(string) (可选) The ID of the parent node.

默认值: false


(string) (可选) The 'href' attribute for the link. If 'href' is not set the node will be a text node.

默认值: false


(boolean) (可选) This will make the node a group (node) if set to 'true'. Group nodes are not visible in the Toolbar, but nodes added to it are. See add_group().

默认值: false


(array) (可选) An array of meta data for the node.

默认值: array()

  • 'html' - The html used for the node.
  • 'class' - The class attribute for the list item containing the link or text node.
  • 'rel' - The rel attribute.
  • 'onclick' - The onclick attribute for the link. This will only be set if the 'href' argument is present.
  • 'target' - The target attribute for the link. This will only be set if the 'href' argument is present.
  • 'title' - The title attribute. Will be set to the link or to a div containing a text node.
  • 'tabindex' - The tabindex attribute. Will be set to the link or to a div containing a text node.

add_node() 参数


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.

/* ----------------------------------
 * wordpress之魂 ©
 * ---------------------------------- */

add_action( 'admin_bar_menu', 'toolbar_link_to_mypage', 999 );

function toolbar_link_to_mypage( $wp_admin_bar ) {
	$args = array(
		'id'    => 'my_page',
		'title' => 'My Page',
		'href'  => '',
		'meta'  => array( 'class' => 'my-toolbar-page' )
	$wp_admin_bar->add_node( $args );

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.

/* ----------------------------------
 * wordpress之魂 ©
 * ---------------------------------- */

add_action( 'admin_bar_menu', 'make_parent_node', 999 );

function make_parent_node( $wp_admin_bar ) {
	$args = array(
		'id'     => 'new-post',     // id of the existing child node (New > Post)
		'title'  => 'Add New Post', // alter the title of existing node
		'parent' => false,          // set parent to false to make it a top level (parent) node
	$wp_admin_bar->add_node( $args );

Single function to add parent/child sub menu items

This is how to add a parent or sub menu item with one function.

/* ----------------------------------
 * wordpress之魂 ©
 * ---------------------------------- */

add_action('wp_before_admin_bar_render', 'baw_admin_bar_render', 100);

 * Adds admin bar items for easy access to the theme creator and editor
function baw_admin_bar_render() {
    baw_admin_bar_render('BAW'); // Parent item
    baw_admin_bar_render('BAW Sub1', 'some_link_to_the_settings', 'BAW');
    baw_admin_bar_render('BAW Sub2', 'some_link_to_the_settings', 'BAW');

 * Adds menu parent or submenu item.
 * @param string $name the label of the menu item
 * @param string $href the link to the item (settings page or ext site)
 * @param string $parent Parent label (if creating a submenu item)
 * @return void
 * */
function baw_admin_bar_render( $name, $href = '', $parent = '', $custom_meta = array() ) {
    global $wp_admin_bar;

    if (!is_super_admin()
            || !function_exists('is_admin_bar_showing')) {
            || !is_admin_bar_showing()
            || !is_object($wp_admin_bar)

    // Generate ID based on the current filename and the name supplied.
    $id = sanitize_key( basename(__FILE__, '.php' ) . '-' . $name );

    // Generate the ID of the parent.
    $parent = sanitize_key( basename(__FILE__, '.php' ) . '-' . $parent );

    // links from the current host will open in the current window

    $meta = strpos( $href, site_url() ) !== false ? array() : array( 'target' => '_blank' ); // external links open in new tab/window
    $meta = array_merge( $meta, $custom_meta );

    $wp_admin_bar->add_node( array(
        'parent' => $parent,
        'id' => $id,
        'title' => $name,
        'href' => $href,
        'meta' => $meta,
    ) );

Sorting The Links on the Admin Menu Bar

Use further array functions in PHP to sort the admin menu items with the following code.

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.

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.

/* ----------------------------------
 * wordpress之魂 ©
 * ---------------------------------- */

add_action( 'admin_bar_menu', 'social_media_links', 900 );
function social_media_links($wp_admin_bar)

	$args = array(
		'id'     => 'social_media',
		'title'	=>	'Social Media',
		'meta'   => array( 'class' => 'first-toolbar-group' ),
	$wp_admin_bar->add_node( $args );	

			$args = array();
				'id'		=>	'twitter',
				'title'		=>	'Twitter',
				'href'		=>	'',
				'parent'	=>	'social_media',

				'id'     	=> 'youtube',
				'title'		=>	'YouTube',
				'href'		=>	'',
				'parent' 	=> 'social_media',
				'meta'   	=> array( 'class' => 'first-toolbar-group' ),

				'id'		=>	'fb',
				'title'		=>	'Facebook',
				'href'		=>	'',
				'parent'	=>	'social_media',


add_node() 示例


add_node() 函数的代码位于 wp-includes/class-wp-admin-bar.php.

add_node() 源文件