WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get nodes

Description

This function returns an array of all the Toolbar items on the current page. Toolbar items are also called "nodes".

The Toolbar replaces the Admin Bar since WordPress Version 3.3.

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.

Usage

<?php $wp_admin_bar->get_nodes(); ?>

Parameters

This tag does not accept any parameters.

Examples

Add a Span Before the Title of All Toolbar Items

This example adds an empty span with the class "my-class" before every Toolbar item's title. Put this in your theme's functions.php file.

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

function all_toolbar_nodes( $wp_admin_bar ) {

	$all_toolbar_nodes = $wp_admin_bar->get_nodes();

	foreach ( $all_toolbar_nodes as $node ) {

		// use the same node's properties
		$args = $node;

		// put a span before the title
		$args->title = '<span class="my-class"></span>' . $node->title;

		// update the Toolbar node
		$wp_admin_bar->add_node( $args );
	}

}

Display all Node ID's of the Current Page in the Toolbar

This example will add all node ID's on the current page to a top-level Toolbar item called "Node ID's". This is for developers who want to find out what the node ID is for a specific node. Put this in your theme's functions.php file.

// use 'wp_before_admin_bar_render' hook to also get nodes produced by plugins.
add_action( 'wp_before_admin_bar_render', 'add_all_node_ids_to_toolbar' );

function add_all_node_ids_to_toolbar() {

	global $wp_admin_bar;
	$all_toolbar_nodes = $wp_admin_bar->get_nodes();

	if ( $all_toolbar_nodes ) {

		// add a top-level Toolbar item called "Node Id's" to the Toolbar
		$args = array(
			'id'    => 'node_ids',
			'title' => 'Node ID\'s'
		);
		$wp_admin_bar->add_node( $args );

		// add all current parent node id's to the top-level node.
		foreach ( $all_toolbar_nodes as $node  ) {
			if ( isset($node->parent) && $node->parent ) {

				$args = array(
					'id'     => 'node_id_'.$node->id, // prefix id with "node_id_" to make it a unique id
					'title'  => $node->id,
					'parent' => 'node_ids'
					// 'href' => $node->href,
				);
				// add parent node to node "node_ids"
				$wp_admin_bar->add_node($args);
			}
		}

		// add all current Toolbar items to their parent node or to the top-level node
		foreach ( $all_toolbar_nodes as $node ) {

			$args = array(
				'id'      => 'node_id_'.$node->id, // prefix id with "node_id_" to make it a unique id
				'title'   => $node->id,
				// 'href' => $node->href,
			);

			if ( isset($node->parent) && $node->parent ) {
				$args['parent'] = 'node_id_'.$node->parent;
			} else {
				$args['parent'] = 'node_ids';
			}

			$wp_admin_bar->add_node($args);
		}
	}
}

Return Values

  • Array with node objects.

Change Log

Source File

get_nodes() is located in wp-includes/class-wp-admin-bar.php.

Related

Toolbar API

See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.