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
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
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
- Scaling
- Constant
- Instructions
- 38–80
- Plugin surface
- 2 hooks
- Called by
- 4
Reaches the database via get_term().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 149.
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.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
!is_wp_error() | 38–44 | wp_get_nav_menu_object(), get_term_by(), is_wp_error() |
!is_wp_error() | 42–53 | wp_get_nav_menu_object(), get_term_by(), is_wp_error(), is_wp_error() |
isset($_menu) | 53–57 | wp_get_nav_menu_object(), get_term_by(), is_wp_error(), wp_update_term(), is_wp_error() |
is_wp_error() | 54–58 | wp_get_nav_menu_object(), get_term_by(), get_term_by(), wp_insert_term(), is_wp_error() |
isset($_menu) | 57–66 | wp_get_nav_menu_object(), get_term_by(), is_wp_error(), is_wp_error(), wp_update_term(), is_wp_error() |
is_wp_error() | 58–67 | wp_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 !== false | 59–63 | wp_get_nav_menu_object(), get_term_by(), is_wp_error(), __(), esc_html(), sprintf(), description() |
| always | 60–64 | wp_get_nav_menu_object(), get_term_by(), get_term_by(), __(), esc_html(), sprintf() |
!is_wp_error() && isset($_menu) | 61–65 | wp_get_nav_menu_object(), get_term_by(), is_wp_error(), wp_update_term(), is_wp_error(), do_action() |
is_wp_error() | 62–71 | wp_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–67 | wp_get_nav_menu_object(), get_term_by(), get_term_by(), wp_insert_term(), is_wp_error(), do_action() |
| always | 64–73 | wp_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–74 | wp_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–76 | wp_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–77 | wp_get_nav_menu_object(), get_term_by(), is_wp_error(), is_wp_error(), get_term_by(), __(), esc_html(), sprintf() |
| always | 71–80 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 149 | 38–80 | 14 | |
| 8.5 | 149 | 38–80 | 14 | |
| 8.4 | 149 | 38–80 | 14 | |
| 8.3 | 149 | 38–80 | 14 | |
| 8.2 | 149 | 38–80 | 14 | |
| 8.1 | 149 | 38–80 | 14 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 152 | 38–83 | 14 |
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:
- do_action( wp_create_nav_menu )actionline 375 (+60 into the body)
Fires after a navigation menu is successfully created.
- do_action( wp_update_nav_menu )actionline 402 (+87 into the body)
Fires after a navigation menu has been successfully updated.
Uses · 9
- wp_get_nav_menu_object()Returns a navigation menu object.
- get_term_by()Gets all term data from database by term field and data.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- esc_html()Escaping for HTML blocks.
- wp_insert_term()Adds a new term to the database.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_update_term()Updates term based on arguments provided.
- WP_Error::__construct()Initializes the error.
Used by · 4
- WP_Customize_Nav_Menu_Setting::update()Create/update the nav_menu term for this setting.
- WP_REST_Menus_Controller::create_item()Creates a single term in a taxonomy.
- WP_REST_Menus_Controller::update_item()Updates a single term from a taxonomy.
- wp_create_nav_menu()Creates a navigation menu.
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.
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.