_wp_menu_item_classes_by_context( array $menu_items )
- Since
- 3.0.0
- Source
wp-includes/nav-menu-template.php:327
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
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
- Scaling
- Scales with input
- Instructions
- 65–67
- Plugin surface
- None
- Called by
- 4
Reaches the database via wp_get_object_terms().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 641.
Nothing here hands control to plugin code.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - querycontent query
wp_get_object_terms()called directly - hookthird-party callbacks
apply_filters()one call below _wp_menu_item_classes_by_context() - cacheobject cache
wp_cache_get()one call below _wp_menu_item_classes_by_context() - serializeserialisation
maybe_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.
| When | Instructions | Calls 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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 641 | 65–67 | 89 | |
| 8.5 | 641 | 65–67 | 89 | |
| 8.4 | 641 | 65–67 | 89 | 52 fewer instructions than PHP 8.3 |
| 8.3 | 693 | 65–67 | 89 | |
| 8.2 | 693 | 65–67 | 89 | 1 more instruction than PHP 8.1 |
| 8.1 | 692 | 65–67 | 89 | 4 fewer instructions than PHP 7.4 |
| 7.4 | 696 | 65–67 | 89 |
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
- get_option()Retrieves an option value based on an option name.
- is_post_type_hierarchical()Determines whether the post type is hierarchical.
- get_object_taxonomies()Returns the names or objects of the taxonomies which are registered for the requested object or object type, such as a post object or post type name.
- is_taxonomy_hierarchical()Determines whether the taxonomy object is hierarchical.
- _get_term_hierarchy()Retrieves children of taxonomy as term IDs.
- wp_get_object_terms()Retrieves the terms associated with the given object(s), in the supplied taxonomies.
- home_url()Retrieves the URL for the current site where the front end is accessible.
- get_post_meta()Retrieves a post meta field for the given post ID.
- is_post_type_archive()Determines whether the query is for an existing post type archive page.
- untrailingslashit()Removes trailing forward slashes and backslashes if they exist.
- is_customize_preview()Whether the site is being previewed in the Customizer.
- set_url_scheme()Sets the scheme for a URL.
Show all 13
- is_front_page()Determines whether the query is for the front page of the site.
Used by · 4
- WP_Classic_To_Block_Menu_Converter::convert()Converts a Classic Menu to blocks.
- block_core_navigation_get_classic_menu_fallback_blocks()Converts a classic navigation to blocks.
- block_core_navigation_get_menu_items_at_location()Returns the menu items for a WordPress menu location.
- wp_nav_menu()Displays a navigation menu.
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.
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.