wp_get_nav_menu_to_edit

Returns the menu formatted to edit.

Description

(string|WP_Error) wp_get_nav_menu_to_edit( (int) $menu_id = 0 ); 

Returns (string|WP_Error)

$output The menu formatted to edit or error object on failure.

Parameters (1)

0. $menu_id — Optional. (int)
The ID of the menu to format. Default 0.

Usage

  1. if ( !function_exists( 'wp_get_nav_menu_to_edit' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/nav-menu.php'; 
  3.  
  4. // Optional. The ID of the menu to format. Default 0. 
  5. $menu_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_get_nav_menu_to_edit($menu_id); 
  9.  

Defined (1)

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

/wp-admin/includes/nav-menu.php  
  1. function wp_get_nav_menu_to_edit( $menu_id = 0 ) { 
  2. $menu = wp_get_nav_menu_object( $menu_id ); 
  3.  
  4. // If the menu exists, get its items. 
  5. if ( is_nav_menu( $menu ) ) { 
  6. $menu_items = wp_get_nav_menu_items( $menu->term_id, array('post_status' => 'any') ); 
  7. $result = '<div id="menu-instructions" class="post-body-plain'; 
  8. $result .= ( ! empty($menu_items) ) ? ' menu-instructions-inactive">' : '">'; 
  9. $result .= '<p>' . __( 'Add menu items from the column on the left.' ) . '</p>'; 
  10. $result .= '</div>'; 
  11.  
  12. if ( empty($menu_items) ) 
  13. return $result . ' <ul class="menu" id="menu-to-edit"> </ul>'; 
  14.  
  15. /** 
  16. * Filters the Walker class used when adding nav menu items. 
  17. * @since 3.0.0 
  18. * @param string $class Thewalkerclass to use. Default 'Walker_Nav_Menu_Edit'. 
  19. * @param int $menu_id ID of the menu being rendered. 
  20. */ 
  21. $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $menu_id ); 
  22.  
  23. if ( class_exists( $walker_class_name ) ) { 
  24. $walker = new $walker_class_name; 
  25. } else { 
  26. return new WP_Error( 'menu_walker_not_exist',  
  27. /** translators: %s:walkerclass name */ 
  28. sprintf( __( 'The Walker class named %s does not exist.' ),  
  29. '<strong>' . $walker_class_name . '</strong>' 
  30. ); 
  31.  
  32. $some_pending_menu_items = $some_invalid_menu_items = false; 
  33. foreach ( (array) $menu_items as $menu_item ) { 
  34. if ( isset( $menu_item->post_status ) && 'draft' == $menu_item->post_status ) 
  35. $some_pending_menu_items = true; 
  36. if ( ! empty( $menu_item->_invalid ) ) 
  37. $some_invalid_menu_items = true; 
  38.  
  39. if ( $some_pending_menu_items ) { 
  40. $result .= '<div class="notice notice-info notice-alt inline"><p>' . __( 'Click Save Menu to make pending menu items public.' ) . '</p></div>'; 
  41.  
  42. if ( $some_invalid_menu_items ) { 
  43. $result .= '<div class="notice notice-error notice-alt inline"><p>' . __( 'There are some invalid menu items. Please check or delete them.' ) . '</p></div>'; 
  44.  
  45. $result .= '<ul class="menu" id="menu-to-edit"> '; 
  46. $result .= walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', $menu_items), 0, (object) array(walker => $walker ) ); 
  47. $result .= ' </ul> '; 
  48. return $result; 
  49. } elseif ( is_wp_error( $menu ) ) { 
  50. return $menu; 
  51.