Codex tools: Log in
Contents |
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.
<?php $wp_admin_bar->add_node( $args ); ?>
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.
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' => 'http://mysite.com/my-page/',
'meta' => array('class' => 'my-toolbar-page')
);
$wp_admin_bar->add_node($args);
}
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.
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);
}
add_node() is located in wp-includes/class-wp-admin-bar.php.
Toolbar Methods: add_node(), remove_node(), add_group(), get_node(), get_nodes(),