wp_ajax_add_menu_item

Ajax handler for adding a menu item.

Description

wp_ajax_add_menu_item(); 

Usage

  1. if ( !function_exists( 'wp_ajax_add_menu_item' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ajax-actions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_ajax_add_menu_item(); 
  7.  

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_add_menu_item() { 
  2. check_ajax_referer( 'add-menu_item', 'menu-settings-column-nonce' ); 
  3.  
  4. if ( ! current_user_can( 'edit_theme_options' ) ) 
  5. wp_die( -1 ); 
  6.  
  7. require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; 
  8.  
  9. // For performance reasons, we omit some object properties from the checklist. 
  10. // The following is a hacky way to restore them when adding non-custom items. 
  11.  
  12. $menu_items_data = array(); 
  13. foreach ( (array) $_POST['menu-item'] as $menu_item_data ) { 
  14. if ( 
  15. ! empty( $menu_item_data['menu-item-type'] ) && 
  16. 'custom' != $menu_item_data['menu-item-type'] && 
  17. ! empty( $menu_item_data['menu-item-object-id'] ) 
  18. ) { 
  19. switch( $menu_item_data['menu-item-type'] ) { 
  20. case 'post_type' : 
  21. $_object = get_post( $menu_item_data['menu-item-object-id'] ); 
  22. break; 
  23.  
  24. case 'post_type_archive' : 
  25. $_object = get_post_type_object( $menu_item_data['menu-item-object'] ); 
  26. break; 
  27.  
  28. case 'taxonomy' : 
  29. $_object = get_term( $menu_item_data['menu-item-object-id'], $menu_item_data['menu-item-object'] ); 
  30. break; 
  31.  
  32. $_menu_items = array_map( 'wp_setup_nav_menu_item', array( $_object ) ); 
  33. $_menu_item = reset( $_menu_items ); 
  34.  
  35. // Restore the missing menu item properties 
  36. $menu_item_data['menu-item-description'] = $_menu_item->description; 
  37.  
  38. $menu_items_data[] = $menu_item_data; 
  39.  
  40. $item_ids = wp_save_nav_menu_items( 0, $menu_items_data ); 
  41. if ( is_wp_error( $item_ids ) ) 
  42. wp_die( 0 ); 
  43.  
  44. $menu_items = array(); 
  45.  
  46. foreach ( (array) $item_ids as $menu_item_id ) { 
  47. $menu_obj = get_post( $menu_item_id ); 
  48. if ( ! empty( $menu_obj->ID ) ) { 
  49. $menu_obj = wp_setup_nav_menu_item( $menu_obj ); 
  50. $menu_obj->label = $menu_obj->title; // don't show "(pending)" in ajax-added items 
  51. $menu_items[] = $menu_obj; 
  52.  
  53. /** This filter is documented in wp-admin/includes/nav-menu.php */ 
  54. $walker_class_name = apply_filters( 'wp_edit_nav_menu_walker', 'Walker_Nav_Menu_Edit', $_POST['menu'] ); 
  55.  
  56. if ( ! class_exists( $walker_class_name ) ) 
  57. wp_die( 0 ); 
  58.  
  59. if ( ! empty( $menu_items ) ) { 
  60. $args = array( 
  61. 'after' => '',  
  62. 'before' => '',  
  63. 'link_after' => '',  
  64. 'link_before' => '',  
  65. walker => new $walker_class_name,  
  66. ); 
  67. echo walk_nav_menu_tree( $menu_items, 0, (object) $args ); 
  68. wp_die();