wppaste
WordPress

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
Maps nav menu locations according to assignments in previously active theme.

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

$new_nav_menu_locationsarray
New nav menu locations assignments.
$old_nav_menu_locationsarray
Old nav menu locations assignments.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
13–31

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

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.

WhenInstructionsCalls it makes
empty($old_nav_menu_locations)13get_registered_nav_menus(), array_intersect_key()
!empty($old_nav_menu_locations)24–31get_registered_nav_menus(), array_intersect_key(), array_keys()
!empty($old_nav_menu_locations)27get_registered_nav_menus(), array_intersect_key(), key(), array_pop()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11413–3128
8.511413–3128
8.411413–31287 fewer instructions than PHP 8.3
8.312113–3128
8.212113–3128
8.112113–3128
7.412113–3128

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

Used by · 2

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.

  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 6.7.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.