Title: WP_Admin_Bar::_bind
Published: April 25, 2014
Last modified: August 20, 2026

---

# WP_Admin_Bar::_bind(): object|null

## In this article

 * [Return](https://developer.wordpress.org/reference/classes/wp_admin_bar/_bind/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_admin_bar/_bind/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_admin_bar/_bind/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_admin_bar/_bind/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_admin_bar/_bind/?output_format=md#wp--skip-link--target)

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_admin_bar/_bind/?output_format=md#return)󠁿

 object|null

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_admin_bar/_bind/?output_format=md#source)󠁿

    ```php
    final protected function _bind() {
    	if ( $this->bound ) {
    		return null;
    	}

    	/*
    	 * Add the root node.
    	 * Clear it first, just in case. Don't mess with The Root.
    	 */
    	$this->remove_node( 'root' );
    	$this->add_node(
    		array(
    			'id'    => 'root',
    			'group' => false,
    		)
    	);

    	// Normalize nodes: define internal 'children' and 'type' properties.
    	foreach ( $this->_get_nodes() as $node ) {
    		$node->children = array();
    		$node->type     = ( $node->group ) ? 'group' : 'item';
    		unset( $node->group );

    		// The Root wants your orphans. No lonely items allowed.
    		if ( ! $node->parent ) {
    			$node->parent = 'root';
    		}
    	}

    	foreach ( $this->_get_nodes() as $node ) {
    		if ( 'root' === $node->id ) {
    			continue;
    		}

    		// Fetch the parent node. If it isn't registered, ignore the node.
    		$parent = $this->_get_node( $node->parent );
    		if ( ! $parent ) {
    			continue;
    		}

    		// Generate the group class (we distinguish between top level and other level groups).
    		$group_class = ( 'root' === $node->parent ) ? 'ab-top-menu' : 'ab-submenu';

    		if ( 'group' === $node->type ) {
    			if ( empty( $node->meta['class'] ) ) {
    				$node->meta['class'] = $group_class;
    			} else {
    				$node->meta['class'] .= ' ' . $group_class;
    			}
    		}

    		// Items in items aren't allowed. Wrap nested items in 'default' groups.
    		if ( 'item' === $parent->type && 'item' === $node->type ) {
    			$default_id = $parent->id . '-default';
    			$default    = $this->_get_node( $default_id );

    			/*
    			 * The default group is added here to allow groups that are
    			 * added before standard menu items to render first.
    			 */
    			if ( ! $default ) {
    				/*
    				 * Use _set_node because add_node can be overloaded.
    				 * Make sure to specify default settings for all properties.
    				 */
    				$this->_set_node(
    					array(
    						'id'       => $default_id,
    						'parent'   => $parent->id,
    						'type'     => 'group',
    						'children' => array(),
    						'meta'     => array(
    							'class' => $group_class,
    						),
    						'title'    => false,
    						'href'     => false,
    					)
    				);
    				$default            = $this->_get_node( $default_id );
    				$parent->children[] = $default;
    			}
    			$parent = $default;

    			/*
    			 * Groups in groups aren't allowed. Add a special 'container' node.
    			 * The container will invisibly wrap both groups.
    			 */
    		} elseif ( 'group' === $parent->type && 'group' === $node->type ) {
    			$container_id = $parent->id . '-container';
    			$container    = $this->_get_node( $container_id );

    			// We need to create a container for this group, life is sad.
    			if ( ! $container ) {
    				/*
    				 * Use _set_node because add_node can be overloaded.
    				 * Make sure to specify default settings for all properties.
    				 */
    				$this->_set_node(
    					array(
    						'id'       => $container_id,
    						'type'     => 'container',
    						'children' => array( $parent ),
    						'parent'   => false,
    						'title'    => false,
    						'href'     => false,
    						'meta'     => array(),
    					)
    				);

    				$container = $this->_get_node( $container_id );

    				// Link the container node if a grandparent node exists.
    				$grandparent = $this->_get_node( $parent->parent );

    				if ( $grandparent ) {
    					$container->parent = $grandparent->id;

    					$index = array_search( $parent, $grandparent->children, true );
    					if ( false === $index ) {
    						$grandparent->children[] = $container;
    					} else {
    						array_splice( $grandparent->children, $index, 1, array( $container ) );
    					}
    				}

    				$parent->parent = $container->id;
    			}

    			$parent = $container;
    		}

    		// Update the parent ID (it might have changed).
    		$node->parent = $parent->id;

    		// Add the node to the tree.
    		$parent->children[] = $node;
    	}

    	$root        = $this->_get_node( 'root' );
    	$this->bound = true;
    	return $root;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-admin-bar.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/class-wp-admin-bar.php#L311)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wp-admin-bar.php#L311-L452)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_admin_bar/_bind/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Admin_Bar::remove_node()](https://developer.wordpress.org/reference/classes/wp_admin_bar/remove_node/)`wp-includes/class-wp-admin-bar.php` |

Remove a node.

  | 
| [WP_Admin_Bar::add_node()](https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/)`wp-includes/class-wp-admin-bar.php` |

Adds a node to the menu.

  | 
| [WP_Admin_Bar::_get_nodes()](https://developer.wordpress.org/reference/classes/wp_admin_bar/_get_nodes/)`wp-includes/class-wp-admin-bar.php` |  | 
| [WP_Admin_Bar::_get_node()](https://developer.wordpress.org/reference/classes/wp_admin_bar/_get_node/)`wp-includes/class-wp-admin-bar.php` |  | 
| [WP_Admin_Bar::_set_node()](https://developer.wordpress.org/reference/classes/wp_admin_bar/_set_node/)`wp-includes/class-wp-admin-bar.php` |  |

| Used by | Description | 
| [WP_Admin_Bar::render()](https://developer.wordpress.org/reference/classes/wp_admin_bar/render/)`wp-includes/class-wp-admin-bar.php` |  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_admin_bar/_bind/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.3.0](https://developer.wordpress.org/reference/since/3.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_admin_bar%2F_bind%2F)
before being able to contribute a note or feedback.