wppaste
WordPress

WP_Admin_Bar::add_node( array $args )

Since
3.1.0, 4.5.0, 6.5.0
Source
wp-includes/class-wp-admin-bar.php:124
Adds a node to the menu.

Compatibility

WordPress
since 6.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$argsarray
Arguments for adding a node.
  • $idstring

    ID of the item.
  • $titlestring

    Title of the node.
  • $parentstring

    Optional. ID of the parent node.
  • $hrefstring

    Optional. Link for the item.
  • $groupbooldefault: false

    Optional. Whether or not the node is a group.
  • $metaarraydefault: empty

    Meta data including the following keys: 'html', 'class', 'rel', 'lang', 'dir', 'onclick', 'target', 'title', 'tabindex', 'menu_title'.

Performance profile

How much work a call to WP_Admin_Bar::add_node() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
11–105

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 106.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()one call below WP_Admin_Bar::add_node()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 68 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Admin_Bar::add_node() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!is_object($args) && empty($args)11–13none
is_object($args) && empty($args)15–17get_object_vars()
is_string($args) && !is_object($args) && empty($args)22func_get_arg(), parent()
is_string($args) && is_object($args) && empty($args)26func_get_arg(), parent(), get_object_vars()
!is_object($args) && !empty($args)30–37->get_node(), wp_parse_args(), ->_set_node()
!is_object($args) && !empty($args)34–41->get_node(), get_object_vars(), wp_parse_args(), ->_set_node()
is_object($args) && !empty($args)34–41get_object_vars(), ->get_node(), wp_parse_args(), ->_set_node()
is_object($args) && !empty($args)38–45get_object_vars(), ->get_node(), get_object_vars(), wp_parse_args(), ->_set_node()
is_string($args) && !is_object($args) && !empty($args)41–46func_get_arg(), parent(), ->get_node(), wp_parse_args(), ->_set_node()
!is_object($args) && !empty($args) && !empty($defaults)42–47->get_node(), wp_parse_args(), wp_parse_args(), ->_set_node()
is_string($args) && !is_object($args) && !empty($args)45–50func_get_arg(), parent(), ->get_node(), get_object_vars(), wp_parse_args(), ->_set_node()
is_string($args) && is_object($args) && !empty($args)45–50func_get_arg(), parent(), get_object_vars(), ->get_node(), wp_parse_args(), ->_set_node()
56 further outcomes, up to 105 instructions
!is_object($args) && !empty($args) && !empty($defaults)46–51->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), ->_set_node()
is_object($args) && !empty($args) && !empty($defaults)46–51get_object_vars(), ->get_node(), wp_parse_args(), wp_parse_args(), ->_set_node()
is_string($args) && is_object($args) && !empty($args)49–54func_get_arg(), parent(), get_object_vars(), ->get_node(), get_object_vars(), wp_parse_args(), ->_set_node()
is_object($args) && !empty($args) && !empty($defaults)50–55get_object_vars(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), ->_set_node()
!is_object($args)50–57__(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), ->_set_node()
is_string($args) && !is_object($args) && !empty($args) && !empty($defaults)53–56func_get_arg(), parent(), ->get_node(), wp_parse_args(), wp_parse_args(), ->_set_node()
!is_object($args) && !empty($args) && isset([…])54–58->get_node(), wp_parse_args(), _deprecated_argument(), ->_set_node()
!is_object($args)54–61__(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), ->_set_node()
is_object($args)54–61get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), ->_set_node()
is_string($args) && !is_object($args) && !empty($args) && !empty($defaults)57–60func_get_arg(), parent(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), ->_set_node()
is_string($args) && is_object($args) && !empty($args) && !empty($defaults)57–60func_get_arg(), parent(), get_object_vars(), ->get_node(), wp_parse_args(), wp_parse_args(), ->_set_node()
!is_object($args) && !empty($args) && isset([…])58–62->get_node(), get_object_vars(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_object($args) && !empty($args) && isset([…])58–62get_object_vars(), ->get_node(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_object($args)58–65get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), ->_set_node()
is_string($args) && is_object($args) && !empty($args) && !empty($defaults)61–64func_get_arg(), parent(), get_object_vars(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), ->_set_node()
is_string($args) && !is_object($args)61–66func_get_arg(), parent(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), ->_set_node()
is_object($args) && !empty($args) && isset([…])62–66get_object_vars(), ->get_node(), get_object_vars(), wp_parse_args(), _deprecated_argument(), ->_set_node()
!is_object($args) && !empty($defaults)62–67__(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), wp_parse_args(), ->_set_node()
is_string($args) && !is_object($args) && !empty($args) && isset([…])65–67func_get_arg(), parent(), ->get_node(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && !is_object($args)65–70func_get_arg(), parent(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), ->_set_node()
is_string($args) && is_object($args)65–70func_get_arg(), parent(), get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), ->_set_node()
!is_object($args) && !empty($args) && !empty($defaults) && isset([…])66–68->get_node(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
!is_object($args) && !empty($defaults)66–71__(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), ->_set_node()
is_object($args) && !empty($defaults)66–71get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), wp_parse_args(), ->_set_node()
is_string($args) && !is_object($args) && !empty($args) && isset([…])69–71func_get_arg(), parent(), ->get_node(), get_object_vars(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && is_object($args) && !empty($args) && isset([…])69–71func_get_arg(), parent(), get_object_vars(), ->get_node(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && is_object($args)69–74func_get_arg(), parent(), get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), ->_set_node()
!is_object($args) && !empty($args) && !empty($defaults) && isset([…])70–72->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_object($args) && !empty($args) && !empty($defaults) && isset([…])70–72get_object_vars(), ->get_node(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_object($args) && !empty($defaults)70–75get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), ->_set_node()
is_string($args) && is_object($args) && !empty($args) && isset([…])73–75func_get_arg(), parent(), get_object_vars(), ->get_node(), get_object_vars(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && !is_object($args) && !empty($defaults)73–76func_get_arg(), parent(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), wp_parse_args(), ->_set_node()
is_object($args) && !empty($args) && !empty($defaults) && isset([…])74–76get_object_vars(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
!is_object($args) && isset([…])74–78__(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && !is_object($args) && !empty($args) && !empty($defaults) && isset([…])77func_get_arg(), parent(), ->get_node(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && !is_object($args) && !empty($defaults)77–80func_get_arg(), parent(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), ->_set_node()
is_string($args) && is_object($args) && !empty($defaults)77–80func_get_arg(), parent(), get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), wp_parse_args(), ->_set_node()
!is_object($args) && isset([…])78–82__(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_object($args) && isset([…])78–82get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && !is_object($args) && !empty($args) && !empty($defaults) && isset([…])81func_get_arg(), parent(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && is_object($args) && !empty($args) && !empty($defaults) && isset([…])81func_get_arg(), parent(), get_object_vars(), ->get_node(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && is_object($args) && !empty($defaults)81–84func_get_arg(), parent(), get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), ->_set_node()
is_object($args) && isset([…])82–86get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && is_object($args) && !empty($args) && !empty($defaults) && isset([…])85func_get_arg(), parent(), get_object_vars(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && !is_object($args) && isset([…])85–87func_get_arg(), parent(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), _deprecated_argument(), ->_set_node()
!is_object($args) && !empty($defaults) && isset([…])86–88__(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && !is_object($args) && isset([…])89–91func_get_arg(), parent(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && is_object($args) && isset([…])89–91func_get_arg(), parent(), get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), _deprecated_argument(), ->_set_node()
!is_object($args) && !empty($defaults) && isset([…])90–92__(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_object($args) && !empty($defaults) && isset([…])90–92get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && is_object($args) && isset([…])93–95func_get_arg(), parent(), get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_object($args) && !empty($defaults) && isset([…])94–96get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && !is_object($args) && !empty($defaults) && isset([…])97func_get_arg(), parent(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && !is_object($args) && !empty($defaults) && isset([…])101func_get_arg(), parent(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && is_object($args) && !empty($defaults) && isset([…])101func_get_arg(), parent(), get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()
is_string($args) && is_object($args) && !empty($defaults) && isset([…])105func_get_arg(), parent(), get_object_vars(), __(), _doing_it_wrong(), sanitize_title(), esc_attr(), ->get_node(), get_object_vars(), wp_parse_args(), wp_parse_args(), _deprecated_argument(), ->_set_node()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10611–10510
8.510611–10510
8.410611–105101 fewer instruction than PHP 8.3
8.310711–10610
8.210711–106102 fewer instructions than PHP 8.1
8.110911–10810
7.410911–10810

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 8

Used by · 3

Source code

	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 ( is_string( $args['parent'] ) && 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 );	}

Changelog

Introduced in 3.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.5.0
Added the ability to pass 'menu_title' for an ARIA menu name.from the docblock
4.5.0
Added the ability to pass 'lang' and 'dir' meta data.from the docblock
3.1.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-admin-bar.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.