wp_save_nav_menu_items

Save posted nav menu item data.

Description

(array) wp_save_nav_menu_items( (int) $menu_id = 0, (array) $menu_data = array() ); 

Returns (array)

The database IDs of the items saved

Parameters (2)

0. $menu_id — Optional. (int)
The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item.
1. $menu_data — Optional. (array) => array()
The unsanitized posted menu item data.

Usage

  1. if ( !function_exists( 'wp_save_nav_menu_items' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/nav-menu.php'; 
  3.  
  4. // The menu ID for which to save this item. $menu_id of 0 makes a draft, orphaned menu item. 
  5. $menu_id = -1; 
  6.  
  7. // The unsanitized posted menu item data. 
  8. $menu_data = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_save_nav_menu_items($menu_id, $menu_data); 
  12.  

Defined (1)

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

/wp-admin/includes/nav-menu.php  
  1. function wp_save_nav_menu_items( $menu_id = 0, $menu_data = array() ) { 
  2. $menu_id = (int) $menu_id; 
  3. $items_saved = array(); 
  4.  
  5. if ( 0 == $menu_id || is_nav_menu( $menu_id ) ) { 
  6.  
  7. // Loop through all the menu items' POST values. 
  8. foreach ( (array) $menu_data as $_possible_db_id => $_item_object_data ) { 
  9. if ( 
  10. // Checkbox is not checked. 
  11. empty( $_item_object_data['menu-item-object-id'] ) && 
  12. // And item type either isn't set. 
  13. ! isset( $_item_object_data['menu-item-type'] ) || 
  14. // Or URL is the default. 
  15. in_array( $_item_object_data['menu-item-url'], array( 'http://', '' ) ) || 
  16. ! ( 'custom' == $_item_object_data['menu-item-type'] && ! isset( $_item_object_data['menu-item-db-id'] ) ) || // or it's not a custom menu item (but not the custom home page) 
  17. // Or it *is* a custom menu item that already exists. 
  18. ! empty( $_item_object_data['menu-item-db-id'] ) 
  19. ) { 
  20. // Then this potential menu item is not getting added to this menu. 
  21. continue; 
  22.  
  23. // If this possible menu item doesn't actually have a menu database ID yet. 
  24. if ( 
  25. empty( $_item_object_data['menu-item-db-id'] ) || 
  26. ( 0 > $_possible_db_id ) || 
  27. $_possible_db_id != $_item_object_data['menu-item-db-id'] 
  28. ) { 
  29. $_actual_db_id = 0; 
  30. } else { 
  31. $_actual_db_id = (int) $_item_object_data['menu-item-db-id']; 
  32.  
  33. $args = array( 
  34. 'menu-item-db-id' => ( isset( $_item_object_data['menu-item-db-id'] ) ? $_item_object_data['menu-item-db-id'] : '' ),  
  35. 'menu-item-object-id' => ( isset( $_item_object_data['menu-item-object-id'] ) ? $_item_object_data['menu-item-object-id'] : '' ),  
  36. 'menu-item-object' => ( isset( $_item_object_data['menu-item-object'] ) ? $_item_object_data['menu-item-object'] : '' ),  
  37. 'menu-item-parent-id' => ( isset( $_item_object_data['menu-item-parent-id'] ) ? $_item_object_data['menu-item-parent-id'] : '' ),  
  38. 'menu-item-position' => ( isset( $_item_object_data['menu-item-position'] ) ? $_item_object_data['menu-item-position'] : '' ),  
  39. 'menu-item-type' => ( isset( $_item_object_data['menu-item-type'] ) ? $_item_object_data['menu-item-type'] : '' ),  
  40. 'menu-item-title' => ( isset( $_item_object_data['menu-item-title'] ) ? $_item_object_data['menu-item-title'] : '' ),  
  41. 'menu-item-url' => ( isset( $_item_object_data['menu-item-url'] ) ? $_item_object_data['menu-item-url'] : '' ),  
  42. 'menu-item-description' => ( isset( $_item_object_data['menu-item-description'] ) ? $_item_object_data['menu-item-description'] : '' ),  
  43. 'menu-item-attr-title' => ( isset( $_item_object_data['menu-item-attr-title'] ) ? $_item_object_data['menu-item-attr-title'] : '' ),  
  44. 'menu-item-target' => ( isset( $_item_object_data['menu-item-target'] ) ? $_item_object_data['menu-item-target'] : '' ),  
  45. 'menu-item-classes' => ( isset( $_item_object_data['menu-item-classes'] ) ? $_item_object_data['menu-item-classes'] : '' ),  
  46. 'menu-item-xfn' => ( isset( $_item_object_data['menu-item-xfn'] ) ? $_item_object_data['menu-item-xfn'] : '' ),  
  47. ); 
  48.  
  49. $items_saved[] = wp_update_nav_menu_item( $menu_id, $_actual_db_id, $args ); 
  50.  
  51. return $items_saved;