On this page
WP_Admin_Bar::add_node( array $args )
Adds a node to the menu.
Parameters
$argsarray Required-
Arguments for adding a node.
idstringID of the item.titlestringTitle of the node.parentstringOptional. ID of the parent node.hrefstringOptional. Link for the item.groupboolOptional. Whether or not the node is a group. Default false.metaarrayMeta data including the following keys:'html','class','rel','lang','dir','onclick','target','title','tabindex'. Default empty.
More Information
- 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.
- add_menu is an alias for this method.
Source
File: wp-includes/class-wp-admin-bar.php. View all references
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 );
}
Related
Uses
| Uses | Description |
|---|---|
| WP_Admin_Bar::get_node() wp-includes/class-wp-admin-bar.php | Gets a node. |
| sanitize_title() wp-includes/formatting.php | Sanitizes a string into a slug, which can be used in URLs or HTML attributes. |
| WP_Admin_Bar::_set_node() wp-includes/class-wp-admin-bar.php | |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| esc_attr() wp-includes/formatting.php | Escaping for HTML attributes. |
| _doing_it_wrong() wp-includes/functions.php | Marks something as being incorrectly called. |
| _deprecated_argument() wp-includes/functions.php | Marks a function argument as deprecated and inform when it has been used. |
| wp_parse_args() wp-includes/functions.php | Merges user defined arguments into defaults array. |
Used By
| Used By | Description |
|---|---|
| wp_admin_bar_edit_site_menu() wp-includes/admin-bar.php | Adds the “Edit site” link to the Toolbar. |
| wp_admin_bar_recovery_mode_menu() wp-includes/admin-bar.php | Adds a link to exit recovery mode when Recovery Mode is active. |
| wp_admin_bar_customize_menu() wp-includes/admin-bar.php | Adds the “Customize” link to the Toolbar. |
| WP_Admin_Bar::_bind() wp-includes/class-wp-admin-bar.php | |
| WP_Admin_Bar::add_menu() wp-includes/class-wp-admin-bar.php | Adds a node (menu item) to the admin bar menu. |
| WP_Admin_Bar::add_group() wp-includes/class-wp-admin-bar.php | Adds a group to a toolbar menu node. |
| wp_admin_bar_wp_menu() wp-includes/admin-bar.php | Adds the WordPress logo menu. |
| wp_admin_bar_sidebar_toggle() wp-includes/admin-bar.php | Adds the sidebar toggle button. |
| wp_admin_bar_my_account_item() wp-includes/admin-bar.php | Adds the “My Account” item. |
| wp_admin_bar_my_account_menu() wp-includes/admin-bar.php | Adds the “My Account” submenu items. |
| wp_admin_bar_site_menu() wp-includes/admin-bar.php | Adds the “Site Name” menu. |
| wp_admin_bar_my_sites_menu() wp-includes/admin-bar.php | Adds the “My Sites/[Site Name]” menu and all submenus. |
| wp_admin_bar_shortlink_menu() wp-includes/admin-bar.php | Provides a shortlink. |
| wp_admin_bar_edit_menu() wp-includes/admin-bar.php | Provides an edit link for posts and terms. |
| wp_admin_bar_new_content_menu() wp-includes/admin-bar.php | Adds “Add New” menu. |
| wp_admin_bar_comments_menu() wp-includes/admin-bar.php | Adds edit comments link with awaiting moderation count bubble. |
| wp_admin_bar_appearance_menu() wp-includes/admin-bar.php | Adds appearance submenu items to the “Site Name” menu. |
| wp_admin_bar_updates_menu() wp-includes/admin-bar.php | Provides an update link if theme/plugin/core updates are available. |
| wp_admin_bar_search_menu() wp-includes/admin-bar.php | Adds search form. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node