Codex

Class Reference/WP Admin Bar

Contents

Role of WP_Admin_Bar

WP_Admin_Bar is WordPress' class for generating the Toolbar that lines the top of of WordPress sites when signed in. This class can be hooked and modified to add or remove options that appear in the admin bar.

The Toolbar replaces the Admin Bar since WordPress Version 3.3.

note: Additional Toolbar functions are available outside the class for use in manipulating the $wp_admin_bar object. See /wp-includes/admin-bar.php or the Related section at the bottom of this article for more information.

Code

The WP_Admin_Bar class is located in the file /wp-includes/class-wp-admin-bar.php

Available Hooks

The following hooks are available for modifying this class:

admin_bar_init
This action hook is called at the very end of initialization.
add_admin_bar_menus
This action hook is called at the end of the public add_menus() method.

Please note that these hooks have very limited use. In many cases, you will simply want to modify the $wp_admin_bar object. See usage below for more information.

Usage

This class is used internally by WordPress to create an object called $wp_admin_bar. Most modifications to WordPress' admin bar will generally be done by modifying the $wp_admin_bar object with the wp_before_admin_bar_render hook, and NOT necessarily through WP_Admin_Bar's internal hooks.

Key Methods

add_node()

This can be used to add a node (such as a new menu) to the admin bar object. It accepts one array containing arguments.

id
REQUIRED. The slug-style id of the node.
title
REQUIRED. The visible name of the menu item.
parent
Optional. The id of the parent node, if this new node is to be a sub-menu item.
href
Optional. An href/url that this menu item should link to.
group
Optional. A boolean specifying whether this node is a group. Default false.
meta
Optional. An associative array specifying any additional HTML attributes to attach to the node. For example: class, onclick, target, title, tabindex, etc.

remove_node()

This method can be used to remove any pre-defined node from the object. It accepts one argument, the id of the node to be removed.

Related

Toolbar Methods: add_node(), remove_node(), add_group(), get_node(), get_nodes(),