WP_Admin_Bar::add_node

Advertisement

Summery Summery

Adds a node to the menu.

Syntax Syntax

WP_Admin_Bar::add_node( array $args )

Parameters Parameters

$args

(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.

Source 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 );
	}

Advertisement

Changelog Changelog

Changelog
Version Description
4.5.0 Added the ability to pass 'lang' and 'dir' meta data.
3.1.0 Introduced.

Advertisement

Leave a Reply