wppaste
WordPress

Walker::display_element( object $element, array $children_elements, int $max_depth, int $depth, array $args, string $output )

Since
2.5.0
Source
wp-includes/class-wp-walker.php:133
Traverses elements to create list from elements.

Description

Display one element if the element doesn't have any children otherwise, display the element and its children. Will only traverse up to the max depth and no ignore elements under that depth. It is possible to set the max depth to include all depths, see walk() method.

This method should not be called directly, use the walk() method instead.

Compatibility

WordPress
since 2.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

$elementobject
Data object.
$children_elementsarray
List of elements to continue traversing (passed by reference).
$max_depthint
Max depth to traverse.
$depthint
Depth of current element.
$argsarray
An array of arguments.
$outputstring
Used to append additional content (passed by reference).

Performance profile

How much work a call to Walker::display_element() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
8–72

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
4

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

What one call costs · 3 distinct outcomes

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

WhenInstructionsCalls it makes
always8none
always47–63array_values(), ->start_el(), array_values(), ->end_el()
isset($newlevel)57–72array_values(), ->start_el(), array_values(), ->end_lvl(), array_values(), ->end_el()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev958–7211
8.5958–7211
8.4958–7211
8.3958–7211
8.2958–7211
8.1958–72114 more instructions than PHP 7.4
7.4918–6911

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 · 5

Used by · 4

Source code

	public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {		if ( ! $element ) {			return;		} 		$max_depth = (int) $max_depth;		$depth     = (int) $depth; 		$id_field = $this->db_fields['id'];		$id       = $element->$id_field; 		// Display this element.		$this->has_children = ! empty( $children_elements[ $id ] );		if ( isset( $args[0] ) && is_array( $args[0] ) ) {			$args[0]['has_children'] = $this->has_children; // Back-compat.		} 		$this->start_el( $output, $element, $depth, ...array_values( $args ) ); 		// Descend only when the depth is right and there are children for this element.		if ( ( 0 === $max_depth || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) { 			foreach ( $children_elements[ $id ] as $child ) { 				if ( ! isset( $newlevel ) ) {					$newlevel = true;					// Start the child delimiter.					$this->start_lvl( $output, $depth, ...array_values( $args ) );				}				$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );			}			unset( $children_elements[ $id ] );		} 		if ( isset( $newlevel ) && $newlevel ) {			// End the child delimiter.			$this->end_lvl( $output, $depth, ...array_values( $args ) );		} 		// End this element.		$this->end_el( $output, $element, $depth, ...array_values( $args ) );	}

Changelog

Introduced in 2.5.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.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/class-wp-walker.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.