wp_nav_menu_update_menu_items

Saves nav menu items.

Description

(array) wp_nav_menu_update_menu_items( (int|string) $nav_menu_selected_id, (string) $nav_menu_selected_title ); 

Returns (array)

$messages The menu updated message

Parameters (2)

0. $nav_menu_selected_id (int|string)
(id, slug, or name ) of the currently-selected menu
1. $nav_menu_selected_title (string)
Title of the currently-selected menu

Usage

  1. if ( !function_exists( 'wp_nav_menu_update_menu_items' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/nav-menu.php'; 
  3.  
  4. // (id, slug, or name ) of the currently-selected menu 
  5. $nav_menu_selected_id = null; 
  6.  
  7. // Title of the currently-selected menu 
  8. $nav_menu_selected_title = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_nav_menu_update_menu_items($nav_menu_selected_id, $nav_menu_selected_title); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/nav-menu.php  
  1. function wp_nav_menu_update_menu_items ( $nav_menu_selected_id, $nav_menu_selected_title ) { 
  2. $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' ) ); 
  3. $messages = array(); 
  4. $menu_items = array(); 
  5. // Index menu items by db ID 
  6. foreach ( $unsorted_menu_items as $_item ) 
  7. $menu_items[$_item->db_id] = $_item; 
  8.  
  9. $post_fields = array( 
  10. 'menu-item-db-id', 'menu-item-object-id', 'menu-item-object',  
  11. 'menu-item-parent-id', 'menu-item-position', 'menu-item-type',  
  12. 'menu-item-title', 'menu-item-url', 'menu-item-description',  
  13. 'menu-item-attr-title', 'menu-item-target', 'menu-item-classes', 'menu-item-xfn' 
  14. ); 
  15.  
  16. // Loop through all the menu items' POST variables 
  17. if ( ! empty( $_POST['menu-item-db-id'] ) ) { 
  18. foreach ( (array) $_POST['menu-item-db-id'] as $_key => $k ) { 
  19.  
  20. // Menu item title can't be blank 
  21. if ( ! isset( $_POST['menu-item-title'][ $_key ] ) || '' == $_POST['menu-item-title'][ $_key ] ) 
  22. continue; 
  23.  
  24. $args = array(); 
  25. foreach ( $post_fields as $field ) 
  26. $args[$field] = isset( $_POST[$field][$_key] ) ? $_POST[$field][$_key] : ''; 
  27.  
  28. $menu_item_db_id = wp_update_nav_menu_item( $nav_menu_selected_id, ( $_POST['menu-item-db-id'][$_key] != $_key ? 0 : $_key ), $args ); 
  29.  
  30. if ( is_wp_error( $menu_item_db_id ) ) { 
  31. $messages[] = '<div id="message" class="error"><p>' . $menu_item_db_id->get_error_message() . '</p></div>'; 
  32. } else { 
  33. unset( $menu_items[ $menu_item_db_id ] ); 
  34.  
  35. // Remove menu items from the menu that weren't in $_POST 
  36. if ( ! empty( $menu_items ) ) { 
  37. foreach ( array_keys( $menu_items ) as $menu_item_id ) { 
  38. if ( is_nav_menu_item( $menu_item_id ) ) { 
  39. wp_delete_post( $menu_item_id ); 
  40.  
  41. // Store 'auto-add' pages. 
  42. $auto_add = ! empty( $_POST['auto-add-pages'] ); 
  43. $nav_menu_option = (array) get_option( 'nav_menu_options' ); 
  44. if ( ! isset( $nav_menu_option['auto_add'] ) ) 
  45. $nav_menu_option['auto_add'] = array(); 
  46. if ( $auto_add ) { 
  47. if ( ! in_array( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) 
  48. $nav_menu_option['auto_add'][] = $nav_menu_selected_id; 
  49. } else { 
  50. if ( false !== ( $key = array_search( $nav_menu_selected_id, $nav_menu_option['auto_add'] ) ) ) 
  51. unset( $nav_menu_option['auto_add'][$key] ); 
  52. // Remove nonexistent/deleted menus 
  53. $nav_menu_option['auto_add'] = array_intersect( $nav_menu_option['auto_add'], wp_get_nav_menus( array( 'fields' => 'ids' ) ) ); 
  54. update_option( 'nav_menu_options', $nav_menu_option ); 
  55.  
  56.  
  57. /** This action is documented in wp-includes/nav-menu.php */ 
  58. do_action( 'wp_update_nav_menu', $nav_menu_selected_id ); 
  59.  
  60. $messages[] = '<div id="message" class="updated notice is-dismissible"><p>' . 
  61. /** translators: %s: nav menu title */ 
  62. sprintf( __( '%s has been updated.' ),  
  63. '<strong>' . $nav_menu_selected_title . '</strong>' 
  64. ) . '</p></div>'; 
  65.  
  66. unset( $menu_items, $unsorted_menu_items ); 
  67.  
  68. return $messages;