wp_delete_nav_menu

Delete a Navigation Menu.

Description

(bool|WP_Error) wp_delete_nav_menu( (string) $menu ); 

Returns (bool|WP_Error)

True on success, false or WP_Error object on failure.

Parameters (1)

0. $menu (string)
Menu ID, slug, or name.

Usage

  1. if ( !function_exists( 'wp_delete_nav_menu' ) ) { 
  2. require_once ABSPATH . WPINC . '/nav-menu.php'; 
  3.  
  4. // Menu ID, slug, or name. 
  5. $menu = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_delete_nav_menu($menu); 
  9.  

Defined (1)

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

/wp-includes/nav-menu.php  
  1. function wp_delete_nav_menu( $menu ) { 
  2. $menu = wp_get_nav_menu_object( $menu ); 
  3. if ( ! $menu ) 
  4. return false; 
  5.  
  6. $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' ); 
  7. if ( ! empty( $menu_objects ) ) { 
  8. foreach ( $menu_objects as $item ) { 
  9. wp_delete_post( $item ); 
  10.  
  11. $result = wp_delete_term( $menu->term_id, 'nav_menu' ); 
  12.  
  13. // Remove this menu from any locations. 
  14. $locations = get_nav_menu_locations(); 
  15. foreach ( $locations as $location => $menu_id ) { 
  16. if ( $menu_id == $menu->term_id ) 
  17. $locations[ $location ] = 0; 
  18. set_theme_mod( 'nav_menu_locations', $locations ); 
  19.  
  20. if ( $result && !is_wp_error($result) ) 
  21.  
  22. /** 
  23. * Fires after a navigation menu has been successfully deleted. 
  24. * @since 3.0.0 
  25. * @param int $term_id ID of the deleted menu. 
  26. */ 
  27. do_action( 'wp_delete_nav_menu', $menu->term_id ); 
  28.  
  29. return $result;