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
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
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
- Scaling
- Scales with input
- Instructions
- 80–102
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via get_posts().
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 174.
Third-party callbacks on 'wp_update_nav_menu' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
do_action()called directly - querycontent query
get_posts()one call below wp_nav_menu_update_menu_items() - cacheobject cache
wp_cache_get()one call below wp_nav_menu_update_menu_items() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($menu_items) && !$value | 80–93 | ID(), 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) && $value | 85–96 | ID(), 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) && !$value | 85–99 | ID(), 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) && $value | 90–102 | ID(), 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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 174 | 80–102 | 20 | |
| 8.5 | 174 | 80–102 | 20 | |
| 8.4 | 174 | 80–102 | 20 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 177 | 83–102 | 20 | |
| 8.2 | 177 | 83–102 | 20 | |
| 8.1 | 177 | 83–102 | 20 | |
| 7.4 | 177 | 83–102 | 20 |
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:
- do_action( wp_update_nav_menu )actionline 1512 (+110 into the body)
Fires after a navigation menu has been successfully updated.
Uses · 12
- wp_get_nav_menu_items()Retrieves all menu items of a navigation menu.
- wp_defer_term_counting()Enables or disables term counting.
- wp_update_nav_menu_item()Saves the properties of a menu item or create a new one.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_get_admin_notice()Creates and returns the markup for an admin notice.
- is_nav_menu_item()Determines whether the given ID is a nav menu item.
- wp_delete_post()Trashes or deletes a post or page.
- get_option()Retrieves an option value based on an option name.
- wp_get_nav_menus()Returns all navigation menu objects.
- update_option()Updates the value of an option that was already added.
- do_action()Calls the callback functions that have been added to an action hook.
- __()Retrieves the translation of $text.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.