wppaste
WordPress

wp_nav_menu_update_menu_items( int|string $nav_menu_selected_id, string $nav_menu_selected_title ): string[]

Since
3.6.0
Source
wp-admin/includes/nav-menu.php:1402
Saves nav menu items.

Compatibility

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

$nav_menu_selected_idint|string
ID, slug, or name of the currently-selected menu.
$nav_menu_selected_titlestring
Title of the currently-selected menu.

Return value

string[]
The menu updated messages.

Performance profile

How much work a call to wp_nav_menu_update_menu_items() 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 get_posts().

Scaling
Scales with input

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

Instructions
81–103

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

Plugin surface
1 hook

Third-party callbacks on 'wp_update_nav_menu' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksdo_action()called directly
  • querycontent queryget_posts()one call below wp_nav_menu_update_menu_items()
  • cacheobject cachewp_cache_get()one call below wp_nav_menu_update_menu_items()
  • serializeserialisationmaybe_unserialize()one call below wp_nav_menu_update_menu_items()

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 · 4 distinct outcomes

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

WhenInstructionsCalls it makes
empty($menu_items) && !$value81–94ID(), wp_defer_term_counting(), get_option(), wp_get_nav_menus(), array_intersect(), update_option(), wp_defer_term_counting(), do_action(), __(), sprintf(), wp_get_admin_notice()
empty($menu_items) && $value86–97ID(), wp_defer_term_counting(), get_option(), array_search(), wp_get_nav_menus(), array_intersect(), update_option(), wp_defer_term_counting(), do_action(), __(), sprintf(), wp_get_admin_notice()
!empty($menu_items) && !$value86–100ID(), wp_defer_term_counting(), array_keys(), get_option(), wp_get_nav_menus(), array_intersect(), update_option(), wp_defer_term_counting(), do_action(), __(), sprintf(), wp_get_admin_notice()
!empty($menu_items) && $value91–103ID(), wp_defer_term_counting(), array_keys(), get_option(), array_search(), wp_get_nav_menus(), array_intersect(), update_option(), wp_defer_term_counting(), do_action(), __(), sprintf(), wp_get_admin_notice()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev17581–10320
8.517581–10320
8.417581–103203 fewer instructions than PHP 8.3
8.317884–10320
8.217884–10320
8.117884–10320
7.417884–10320

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.

Hooks and filters fired · 1

One hook fires while wp_nav_menu_update_menu_items() runs, in this order:

  1. do_action( wp_update_nav_menu )actionline 1512 (+110 into the body)

    Fires after a navigation menu has been successfully updated.

Uses · 12

Source code

function wp_nav_menu_update_menu_items( $nav_menu_selected_id, $nav_menu_selected_title ) {	$unsorted_menu_items = wp_get_nav_menu_items(		$nav_menu_selected_id,		array(			'orderby'     => 'ID',			'output'      => ARRAY_A,			'output_key'  => 'ID',			'post_status' => 'draft,publish',		)	); 	$messages   = array();	$menu_items = array(); 	// Index menu items by DB ID.	foreach ( $unsorted_menu_items as $_item ) {		$menu_items[ $_item->db_id ] = $_item;	} 	$post_fields = array(		'menu-item-db-id',		'menu-item-object-id',		'menu-item-object',		'menu-item-parent-id',		'menu-item-position',		'menu-item-type',		'menu-item-title',		'menu-item-url',		'menu-item-description',		'menu-item-attr-title',		'menu-item-target',		'menu-item-classes',		'menu-item-xfn',	); 	wp_defer_term_counting( true ); 	// Loop through all the menu items' POST variables.	if ( ! empty( $_POST['menu-item-db-id'] ) ) {		foreach ( (array) $_POST['menu-item-db-id'] as $_key => $k ) { 			// Menu item title can't be blank.			if ( ! isset( $_POST['menu-item-title'][ $_key ] ) || '' === $_POST['menu-item-title'][ $_key ] ) {				continue;			} 			$args = array();			foreach ( $post_fields as $field ) {				$args[ $field ] = $_POST[ $field ][ $_key ] ?? '';			} 			$menu_item_db_id = wp_update_nav_menu_item(				$nav_menu_selected_id,				( (int) $_POST['menu-item-db-id'][ $_key ] !== $_key ? 0 : $_key ),				$args			); 			if ( is_wp_error( $menu_item_db_id ) ) {				$messages[] = wp_get_admin_notice(					$menu_item_db_id->get_error_message(),					array(						'id'                 => 'message',						'additional_classes' => array( 'error' ),					)				);			} else {				unset( $menu_items[ $menu_item_db_id ] );			}		}	} 	// Remove menu items from the menu that weren't in $_POST.	if ( ! empty( $menu_items ) ) {		foreach ( array_keys( $menu_items ) as $menu_item_id ) {			if ( is_nav_menu_item( $menu_item_id ) ) {				wp_delete_post( $menu_item_id );			}		}	}

Changelog

Introduced in 3.6.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-admin/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.