wppaste
WordPress

_wp_menu_item_classes_by_context( array $menu_items )

Since
3.0.0
Source
wp-includes/nav-menu-template.php:327
Adds the class property classes for the current context, if applicable.

Compatibility

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

$menu_itemsarray
The current menu item objects to which to add the class property information.

Performance profile

How much work a call to _wp_menu_item_classes_by_context() 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
Heavy

Reaches the database via wp_get_object_terms().

Scaling
Scales with input

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

Instructions
65–67

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

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 it touches

  • optionoption read or writeget_option()called directly
  • querycontent querywp_get_object_terms()called directly
  • hookthird-party callbacksapply_filters()one call below _wp_menu_item_classes_by_context()
  • cacheobject cachewp_cache_get()one call below _wp_menu_item_classes_by_context()
  • serializeserialisationmaybe_unserialize()one call below _wp_menu_item_classes_by_context()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
!$wp_query && empty($queried_object)65–67->get_queried_object(), get_option(), array_filter(), home_url(), get_option(), get_option(), array_unique(), array_filter(), array_unique(), array_filter(), array_unique(), array_filter()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev64165–6789
8.564165–6789
8.464165–678952 fewer instructions than PHP 8.3
8.369365–6789
8.269365–67891 more instruction than PHP 8.1
8.169265–67894 fewer instructions than PHP 7.4
7.469665–6789

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

Show all 13
  • is_front_page()Determines whether the query is for the front page of the site.

Used by · 4

Source code

function _wp_menu_item_classes_by_context( &$menu_items ) {	global $wp_query, $wp_rewrite; 	$queried_object    = $wp_query->get_queried_object();	$queried_object_id = (int) $wp_query->queried_object_id; 	$active_object               = '';	$active_ancestor_item_ids    = array();	$active_parent_item_ids      = array();	$active_parent_object_ids    = array();	$possible_taxonomy_ancestors = array();	$possible_object_parents     = array();	$home_page_id                = (int) get_option( 'page_for_posts' ); 	if ( $wp_query->is_singular && ! empty( $queried_object->post_type ) && ! is_post_type_hierarchical( $queried_object->post_type ) ) {		foreach ( (array) get_object_taxonomies( $queried_object->post_type ) as $taxonomy ) {			if ( is_taxonomy_hierarchical( $taxonomy ) ) {				$term_hierarchy = _get_term_hierarchy( $taxonomy );				$terms          = wp_get_object_terms( $queried_object_id, $taxonomy, array( 'fields' => 'ids' ) );				if ( is_array( $terms ) ) {					$possible_object_parents = array_merge( $possible_object_parents, $terms );					$term_to_ancestor        = array();					foreach ( (array) $term_hierarchy as $ancestor => $descendents ) {						foreach ( (array) $descendents as $desc ) {							$term_to_ancestor[ $desc ] = $ancestor;						}					} 					foreach ( $terms as $desc ) {						do {							$possible_taxonomy_ancestors[ $taxonomy ][] = $desc;							if ( isset( $term_to_ancestor[ $desc ] ) ) {								$_desc = $term_to_ancestor[ $desc ];								unset( $term_to_ancestor[ $desc ] );								$desc = $_desc;							} else {								$desc = 0;							}						} while ( ! empty( $desc ) );					}				}			}		}	} elseif ( ! empty( $queried_object->taxonomy ) && is_taxonomy_hierarchical( $queried_object->taxonomy ) ) {		$term_hierarchy   = _get_term_hierarchy( $queried_object->taxonomy );		$term_to_ancestor = array();		foreach ( (array) $term_hierarchy as $ancestor => $descendents ) {			foreach ( (array) $descendents as $desc ) {				$term_to_ancestor[ $desc ] = $ancestor;			}		}		$desc = $queried_object->term_id;		do {			$possible_taxonomy_ancestors[ $queried_object->taxonomy ][] = $desc;			if ( isset( $term_to_ancestor[ $desc ] ) ) {				$_desc = $term_to_ancestor[ $desc ];				unset( $term_to_ancestor[ $desc ] );				$desc = $_desc;			} else {				$desc = 0;			}		} while ( ! empty( $desc ) );	} 	$possible_object_parents = array_filter( $possible_object_parents ); 	$front_page_url         = home_url();	$front_page_id          = (int) get_option( 'page_on_front' );	$privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' ); 	foreach ( (array) $menu_items as $key => $menu_item ) { 		$menu_items[ $key ]->current = false; 		$classes   = (array) $menu_item->classes;		$classes[] = 'menu-item';		$classes[] = 'menu-item-type-' . $menu_item->type;		$classes[] = 'menu-item-object-' . $menu_item->object; 		// This menu item is set as the 'Front Page'.

Changelog

Introduced in 3.0.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.1.0 tag, from src/wp-includes/nav-menu-template.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.