wp_update_nav_menu_object

Save the properties of a menu or create a new menu with those properties.

Description

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

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

Returns (int|WP_Error)

Menu ID on success, WP_Error object on failure.

Parameters (2)

0. $menu_id — Optional. (int)
The ID of the menu or 0 to create a new menu.
1. $menu_data — Optional. (array) => array()
The array of menu data.

Usage

  1. if ( !function_exists( 'wp_update_nav_menu_object' ) ) { 
  2. require_once ABSPATH . WPINC . '/nav-menu.php'; 
  3.  
  4. // The ID of the menu or "0" to create a new menu. 
  5. $menu_id = -1; 
  6.  
  7. // The array of menu data. 
  8. $menu_data = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_update_nav_menu_object($menu_id, $menu_data); 
  12.  

Defined (1)

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

/wp-includes/nav-menu.php  
  1. function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) { 
  2. // expected_slashed ($menu_data) 
  3. $menu_id = (int) $menu_id; 
  4.  
  5. $_menu = wp_get_nav_menu_object( $menu_id ); 
  6.  
  7. $args = array( 
  8. 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),  
  9. 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),  
  10. 'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),  
  11. 'slug' => null,  
  12. ); 
  13.  
  14. // double-check that we're not going to have one menu take the name of another 
  15. $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' ); 
  16. if ( 
  17. $_possible_existing && 
  18. ! is_wp_error( $_possible_existing ) && 
  19. isset( $_possible_existing->term_id ) && 
  20. $_possible_existing->term_id != $menu_id 
  21. ) { 
  22. return new WP_Error( 'menu_exists',  
  23. /** translators: %s: menu name */ 
  24. sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),  
  25. '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>' 
  26. ); 
  27.  
  28. // menu doesn't already exist, so create a new menu 
  29. if ( ! $_menu || is_wp_error( $_menu ) ) { 
  30. $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' ); 
  31.  
  32. if ( $menu_exists ) { 
  33. return new WP_Error( 'menu_exists',  
  34. /** translators: %s: menu name */ 
  35. sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),  
  36. '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>' 
  37. ); 
  38.  
  39. $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args ); 
  40.  
  41. if ( is_wp_error( $_menu ) ) 
  42. return $_menu; 
  43.  
  44. /** 
  45. * Fires after a navigation menu is successfully created. 
  46. * @since 3.0.0 
  47. * @param int $term_id ID of the new menu. 
  48. * @param array $menu_data An array of menu data. 
  49. */ 
  50. do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data ); 
  51.  
  52. return (int) $_menu['term_id']; 
  53.  
  54. if ( ! $_menu || ! isset( $_menu->term_id ) ) 
  55. return 0; 
  56.  
  57. $menu_id = (int) $_menu->term_id; 
  58.  
  59. $update_response = wp_update_term( $menu_id, 'nav_menu', $args ); 
  60.  
  61. if ( is_wp_error( $update_response ) ) 
  62. return $update_response; 
  63.  
  64. $menu_id = (int) $update_response['term_id']; 
  65.  
  66. /** 
  67. * Fires after a navigation menu has been successfully updated. 
  68. * @since 3.0.0 
  69. * @param int $menu_id ID of the updated menu. 
  70. * @param array $menu_data An array of menu data. 
  71. */ 
  72. do_action( 'wp_update_nav_menu', $menu_id, $menu_data ); 
  73. return $menu_id;