wp_map_nav_menu_locations( array $new_nav_menu_locations, array $old_nav_menu_locations ): array
- Since
- 4.9.0
- Source
wp-includes/nav-menu.php:1233
Compatibility
- WordPress
- since 4.9.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
Return value
array- Nav menus mapped to new nav menu locations.
Performance profile
How much work a call to wp_map_nav_menu_locations() 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
- Scaling
- Scales with input
- Instructions
- 13–31
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
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 114.
Nothing here hands control to plugin code.
2 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 wp_map_nav_menu_locations() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($old_nav_menu_locations) | 13 | get_registered_nav_menus(), array_intersect_key() |
!empty($old_nav_menu_locations) | 24–31 | get_registered_nav_menus(), array_intersect_key(), array_keys() |
!empty($old_nav_menu_locations) | 27 | get_registered_nav_menus(), array_intersect_key(), key(), array_pop() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 114 | 13–31 | 28 | |
| 8.5 | 114 | 13–31 | 28 | |
| 8.4 | 114 | 13–31 | 28 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 121 | 13–31 | 28 | |
| 8.2 | 121 | 13–31 | 28 | |
| 8.1 | 121 | 13–31 | 28 | |
| 7.4 | 121 | 13–31 | 28 |
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 · 2
- get_registered_nav_menus()Retrieves all registered navigation menu locations in a theme.
- stripos()
Used by · 2
- WP_Customize_Nav_Menus::customize_register()Adds the customizer settings and controls.
- _wp_menus_changed()Handles menu config after theme change.
Source code
function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations ) { $registered_nav_menus = get_registered_nav_menus(); $new_nav_menu_locations = array_intersect_key( $new_nav_menu_locations, $registered_nav_menus ); // Short-circuit if there are no old nav menu location assignments to map. if ( empty( $old_nav_menu_locations ) ) { return $new_nav_menu_locations; } // If old and new theme have just one location, map it and we're done. if ( 1 === count( $old_nav_menu_locations ) && 1 === count( $registered_nav_menus ) ) { $new_nav_menu_locations[ key( $registered_nav_menus ) ] = array_pop( $old_nav_menu_locations ); return $new_nav_menu_locations; } $old_locations = array_keys( $old_nav_menu_locations ); // Map locations with the same slug. foreach ( $registered_nav_menus as $location => $name ) { if ( in_array( $location, $old_locations, true ) ) { $new_nav_menu_locations[ $location ] = $old_nav_menu_locations[ $location ]; unset( $old_nav_menu_locations[ $location ] ); } } // If there are no old nav menu locations left, then we're done. if ( empty( $old_nav_menu_locations ) ) { return $new_nav_menu_locations; } /* * If old and new theme both have locations that contain phrases * from within the same group, make an educated guess and map it. */ $common_slug_groups = array( array( 'primary', 'menu-1', 'main', 'header', 'navigation', 'top' ), array( 'secondary', 'menu-2', 'footer', 'subsidiary', 'bottom' ), array( 'social' ), ); // Go through each group... foreach ( $common_slug_groups as $slug_group ) { // ...and see if any of these slugs... foreach ( $slug_group as $slug ) { // ...and any of the new menu locations... foreach ( $registered_nav_menus as $new_location => $name ) { // ...actually match! if ( is_string( $new_location ) && false === stripos( $new_location, $slug ) && false === stripos( $slug, $new_location ) ) { continue; } elseif ( is_numeric( $new_location ) && $new_location !== $slug ) { continue; } // Then see if any of the old locations... foreach ( $old_nav_menu_locations as $location => $menu_id ) { // ...and any slug in the same group... foreach ( $slug_group as $slug ) { // ... have a match as well. if ( is_string( $location ) && false === stripos( $location, $slug ) && false === stripos( $slug, $location ) ) { continue; } elseif ( is_numeric( $location ) && $location !== $slug ) { continue; } // Make sure this location wasn't mapped and removed previously. if ( ! empty( $old_nav_menu_locations[ $location ] ) ) { // We have a match that can be mapped! $new_nav_menu_locations[ $new_location ] = $old_nav_menu_locations[ $location ]; // Remove the mapped location so it can't be mapped again. unset( $old_nav_menu_locations[ $location ] ); // Go back and check the next new menu location. continue 3;Changelog
Introduced in 4.9.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 6.9.7 tag, from
src/wp-includes/nav-menu.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.