wppaste
WordPress

wp_update_nav_menu_object( int $menu_id = 0, array $menu_data = array() ): int|WP_Error

Since
3.0.0
Source
wp-includes/nav-menu.php:315
Saves the properties of a menu or create a new menu with those properties.

Description

Note that $menu_data is expected to be pre-slashed.

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_idintoptional
The ID of the menu or "0" to create a new menu.Default: 0
$menu_dataarrayoptional
The array of menu data.Default: array()

Return value

int|WP_Error
Menu ID on success, WP_Error object on failure.

Performance profile

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

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
38–80

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

Plugin surface
2 hooks

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

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()called directly
  • querycontent queryget_term()one call below wp_update_nav_menu_object()

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 16 distinct outcomes

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

WhenInstructionsCalls it makes
!is_wp_error()38–44wp_get_nav_menu_object(), get_term_by(), is_wp_error()
!is_wp_error()42–53wp_get_nav_menu_object(), get_term_by(), is_wp_error(), is_wp_error()
isset($_menu)53–57wp_get_nav_menu_object(), get_term_by(), is_wp_error(), wp_update_term(), is_wp_error()
is_wp_error()54–58wp_get_nav_menu_object(), get_term_by(), get_term_by(), wp_insert_term(), is_wp_error()
isset($_menu)57–66wp_get_nav_menu_object(), get_term_by(), is_wp_error(), is_wp_error(), wp_update_term(), is_wp_error()
is_wp_error()58–67wp_get_nav_menu_object(), get_term_by(), is_wp_error(), get_term_by(), wp_insert_term(), is_wp_error()
!is_wp_error() && isset($_possible_existing) && $menu_id !== false59–63wp_get_nav_menu_object(), get_term_by(), is_wp_error(), __(), esc_html(), sprintf(), description()
always60–64wp_get_nav_menu_object(), get_term_by(), get_term_by(), __(), esc_html(), sprintf()
!is_wp_error() && isset($_menu)61–65wp_get_nav_menu_object(), get_term_by(), is_wp_error(), wp_update_term(), is_wp_error(), do_action()
is_wp_error()62–71wp_get_nav_menu_object(), get_term_by(), is_wp_error(), is_wp_error(), get_term_by(), wp_insert_term(), is_wp_error()
!is_wp_error()63–67wp_get_nav_menu_object(), get_term_by(), get_term_by(), wp_insert_term(), is_wp_error(), do_action()
always64–73wp_get_nav_menu_object(), get_term_by(), is_wp_error(), get_term_by(), __(), esc_html(), sprintf()
4 further outcomes, up to 80 instructions
!is_wp_error() && isset($_menu)65–74wp_get_nav_menu_object(), get_term_by(), is_wp_error(), is_wp_error(), wp_update_term(), is_wp_error(), do_action()
!is_wp_error()67–76wp_get_nav_menu_object(), get_term_by(), is_wp_error(), get_term_by(), wp_insert_term(), is_wp_error(), do_action()
is_wp_error()68–77wp_get_nav_menu_object(), get_term_by(), is_wp_error(), is_wp_error(), get_term_by(), __(), esc_html(), sprintf()
always71–80wp_get_nav_menu_object(), get_term_by(), is_wp_error(), is_wp_error(), get_term_by(), wp_insert_term(), is_wp_error(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14938–8014
8.514938–8014
8.414938–8014
8.314938–8014
8.214938–8014
8.114938–80143 fewer instructions than PHP 7.4
7.415238–8314

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

2 hooks fire while wp_update_nav_menu_object() runs, in this order:

  1. do_action( wp_create_nav_menu )actionline 375 (+60 into the body)

    Fires after a navigation menu is successfully created.

  2. do_action( wp_update_nav_menu )actionline 402 (+87 into the body)

    Fires after a navigation menu has been successfully updated.

Uses · 9

Used by · 4

Source code

function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {	// expected_slashed ($menu_data)	$menu_id = (int) $menu_id; 	$_menu = wp_get_nav_menu_object( $menu_id ); 	$args = array(		'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),		'name'        => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),		'parent'      => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),		'slug'        => null,	); 	// Double-check that we're not going to have one menu take the name of another.	$_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );	if (		$_possible_existing &&		! is_wp_error( $_possible_existing ) &&		isset( $_possible_existing->term_id ) &&		$_possible_existing->term_id !== $menu_id	) {		return new WP_Error(			'menu_exists',			sprintf(				/* translators: %s: Menu name. */				__( 'The menu name %s conflicts with another menu name. Please try another.' ),				'<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'			)		);	} 	// Menu doesn't already exist, so create a new menu.	if ( ! $_menu || is_wp_error( $_menu ) ) {		$menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' ); 		if ( $menu_exists ) {			return new WP_Error(				'menu_exists',				sprintf(					/* translators: %s: Menu name. */					__( 'The menu name %s conflicts with another menu name. Please try another.' ),					'<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'				)			);		} 		$_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args ); 		if ( is_wp_error( $_menu ) ) {			return $_menu;		} 		/**		 * Fires after a navigation menu is successfully created.		 *		 * @since 3.0.0		 *		 * @param int   $term_id   ID of the new menu.		 * @param array $menu_data An array of menu data.		 */		do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data ); 		return (int) $_menu['term_id'];	} 	if ( ! $_menu || ! isset( $_menu->term_id ) ) {		return 0;	} 	$menu_id = (int) $_menu->term_id; 	$update_response = wp_update_term( $menu_id, 'nav_menu', $args ); 	if ( is_wp_error( $update_response ) ) {		return $update_response;	} 	$menu_id = (int) $update_response['term_id']; 	/**

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