wp_ajax_menu_quick_search

Ajax handler for menu quick searching.

Description

_wp_ajax_menu_quick_search( (array) $request = array() ); 

Parameters (1)

0. $request — Optional. (array) => array()
The unsanitized request values.

Usage

  1. if ( !function_exists( '_wp_ajax_menu_quick_search' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/nav-menu.php'; 
  3.  
  4. // The unsanitized request values. 
  5. $request = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = _wp_ajax_menu_quick_search($request); 
  9.  

Defined (2)

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

/wp-admin/includes/nav-menu.php  
  1. function _wp_ajax_menu_quick_search( $request = array() ) { 
  2. $args = array(); 
  3. $type = isset( $request['type'] ) ? $request['type'] : ''; 
  4. $object_type = isset( $request['object_type'] ) ? $request['object_type'] : ''; 
  5. $query = isset( $request['q'] ) ? $request['q'] : ''; 
  6. $response_format = isset( $request['response-format'] ) && in_array( $request['response-format'], array( 'json', 'markup' ) ) ? $request['response-format'] : 'json'; 
  7.  
  8. if ( 'markup' == $response_format ) { 
  9. $args[walker] = new Walker_Nav_Menu_Checklist; 
  10.  
  11. if ( 'get-post-item' == $type ) { 
  12. if ( post_type_exists( $object_type ) ) { 
  13. if ( isset( $request['ID'] ) ) { 
  14. $object_id = (int) $request['ID']; 
  15. if ( 'markup' == $response_format ) { 
  16. echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $object_id ) ) ), 0, (object) $args ); 
  17. } elseif ( 'json' == $response_format ) { 
  18. array( 
  19. 'ID' => $object_id,  
  20. 'post_title' => get_the_title( $object_id ),  
  21. 'post_type' => get_post_type( $object_id ),  
  22. ); 
  23. echo "\n"; 
  24. } elseif ( taxonomy_exists( $object_type ) ) { 
  25. if ( isset( $request['ID'] ) ) { 
  26. $object_id = (int) $request['ID']; 
  27. if ( 'markup' == $response_format ) { 
  28. echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_term( $object_id, $object_type ) ) ), 0, (object) $args ); 
  29. } elseif ( 'json' == $response_format ) { 
  30. $post_obj = get_term( $object_id, $object_type ); 
  31. array( 
  32. 'ID' => $object_id,  
  33. 'post_title' => $post_obj->name,  
  34. 'post_type' => $object_type,  
  35. ); 
  36. echo "\n"; 
  37.  
  38.  
  39. } elseif ( preg_match('/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches) ) { 
  40. if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) { 
  41. $post_type_obj = _wp_nav_menu_meta_box_object( get_post_type_object( $matches[2] ) ); 
  42. $args = array_merge( 
  43. $args,  
  44. array( 
  45. 'no_found_rows' => true,  
  46. 'update_post_meta_cache' => false,  
  47. 'update_post_term_cache' => false,  
  48. 'posts_per_page' => 10,  
  49. 'post_type' => $matches[2],  
  50. 's' => $query,  
  51. ); 
  52. if ( isset( $post_type_obj->_default_query ) ) { 
  53. $args = array_merge( $args, (array) $post_type_obj->_default_query ); 
  54. $search_results_query = new WP_Query( $args ); 
  55. if ( ! $search_results_query->have_posts() ) { 
  56. return; 
  57. while ( $search_results_query->have_posts() ) { 
  58. $post = $search_results_query->next_post(); 
  59. if ( 'markup' == $response_format ) { 
  60. $var_by_ref = $post->ID; 
  61. echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $var_by_ref ) ) ), 0, (object) $args ); 
  62. } elseif ( 'json' == $response_format ) { 
  63. array( 
  64. 'ID' => $post->ID,  
  65. 'post_title' => get_the_title( $post->ID ),  
  66. 'post_type' => $matches[2],  
  67. ); 
  68. echo "\n"; 
  69. } elseif ( 'taxonomy' == $matches[1] ) { 
  70. $terms = get_terms( $matches[2], array( 
  71. 'name__like' => $query,  
  72. 'number' => 10,  
  73. )); 
  74. if ( empty( $terms ) || is_wp_error( $terms ) ) 
  75. return; 
  76. foreach ( (array) $terms as $term ) { 
  77. if ( 'markup' == $response_format ) { 
  78. echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args ); 
  79. } elseif ( 'json' == $response_format ) { 
  80. array( 
  81. 'ID' => $term->term_id,  
  82. 'post_title' => $term->name,  
  83. 'post_type' => $matches[2],  
  84. ); 
  85. echo "\n"; 
/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_menu_quick_search() { 
  2. if ( ! current_user_can( 'edit_theme_options' ) ) 
  3. wp_die( -1 ); 
  4.  
  5. require_once ABSPATH . 'wp-admin/includes/nav-menu.php'; 
  6.  
  7. _wp_ajax_menu_quick_search( $_POST ); 
  8.  
  9. wp_die();