WP_Admin_Bar::add_node() WordPress Method
The WP_Admin_Bar::add_node() method is used to add a new node to the admin bar. This method accepts two parameters: $args (array) – An array of arguments. $node (stdClass|WP_Admin_Bar_Node) – A WP_Admin_Bar_Node object. The $args array can contain the following keys: id – The id of the new node. title – The title of the new node. href – The link for the new node. parent – The id of the parent node. group – The id of the group the new node should be added to. meta – An array of meta data for the new node. class – The class attribute for the new node. The $node parameter can be used to pass a WP_Admin_Bar_Node object. This can be useful when you want to add a node with a custom method attached. Once the new node is added, it will be displayed in the admin bar according to the parent/child relationship and the order of the nodes.
WP_Admin_Bar::add_node( array $args ) #
Adds a node to the menu.
Parameters
- $args
(array)(Required)Arguments for adding a node.
- 'id'
(string) ID of the item. - 'title'
(string) Title of the node. - 'parent'
(string) Optional. ID of the parent node. - 'href'
(string) Optional. Link for the item. - 'group'
(bool) Optional. Whether or not the node is a group. Default false. - 'meta'
(array) Meta data including the following keys: 'html', 'class', 'rel', 'lang', 'dir', 'onclick', 'target', 'title', 'tabindex'. Default empty.
- 'id'
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
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 ); }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
4.5.0 | Added the ability to pass 'lang' and 'dir' meta data. |
3.1.0 | Introduced. |