Ticket #542: autocomplete.diff
File autocomplete.diff, 4.5 KB (added by , 10 years ago) |
---|
-
functions.php
87 87 88 88 add_filter( 'breadcrumb_trail_items', __NAMESPACE__ . '\\breadcrumb_trail_items', 10, 2 ); 89 89 90 add_action( 'wp_ajax_wporg_ajax_search', __NAMESPACE__ . '\\wporg_ajax_search' ); 91 add_action( 'wp_ajax_nopriv_wporg_ajax_search', __NAMESPACE__ . '\\wporg_ajax_search' ); 92 90 93 } 91 94 92 95 … … 429 432 wp_enqueue_style( 'open-sans', '//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,400,300,600' ); 430 433 wp_enqueue_style( 'wporg-developer-style', get_stylesheet_uri(), array(), '2' ); 431 434 wp_enqueue_style( 'wp-dev-sass-compiled', get_template_directory_uri() . '/stylesheets/main.css', array( 'wporg-developer-style' ), '20141026' ); 435 wp_enqueue_style( 'jquery-ui-styles','http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css' ); 432 436 wp_enqueue_script( 'wporg-developer-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true ); 433 437 wp_enqueue_script( 'wporg-developer-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true ); 438 wp_enqueue_script( 'jquery-ui-autocomplete', '', array('jquery-ui-widget', 'jquery-ui-position'), '1.8.6' ); 439 wp_register_script( 'wporg-autocomplete', get_template_directory_uri() .'/js/autocomplete.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-autocomplete' ), '0.1', true ); 440 wp_localize_script( 'wporg-autocomplete', 'WPorgAutocomplete', array( 'search_url' => admin_url( 'admin-ajax.php' ), 'search_action' => 'wporg_ajax_search' ) ); 441 wp_enqueue_script( 'wporg-autocomplete' ); 434 442 } 435 443 436 444 /** … … 565 573 566 574 return $content; 567 575 } 576 577 /** 578 * Ajax search for autocomplete 579 * 580 * @param string $term The string to search for 581 * @return string 582 */ 583 function wporg_ajax_search() { 584 585 parse_str( $_POST['data'], $data ); 586 587 // Sanitize our post type values. 588 if ( ! empty( $data['post_type'] ) ) { 589 if ( is_array( $data['post_type'] ) ) { 590 $clean_types = array_map( 'sanitize_text_field', $data['post_type'] ); 591 } else { 592 $clean_types = sanitize_text_field( $data['post_type'] ); 593 } 594 } else { 595 // If there's no filter set, then we want to search all of them. 596 $clean_types = array( 'wp-parser-hook', 'wp-parser-function', 'wp-parser-method', 'wp-parser-class' ); 597 } 598 599 // Sanitize our search terms. 600 $clean_term = sanitize_text_field( strtolower( $data['s'] ) ); 601 602 $suggestions = array(); 603 604 $loop = new \WP_Query( array( 605 's' => $clean_term, 606 'post_type' => $clean_types, 607 ) ); 608 609 while( $loop->have_posts() ) { 610 $loop->the_post(); 611 $suggestion = array(); 612 $suggestion['label'] = esc_html( get_the_title() ); 613 $suggestion['link'] = esc_url( get_permalink() ); 614 $suggestions[] = $suggestion; 615 } 616 617 wp_reset_query(); 618 619 $response = json_encode( $suggestions ); 620 echo $response; 621 exit(); 622 } 623 -
js/autocomplete.js
1 ( function( $ ) { 2 $( document ).ready( function() { 3 $( 'input#s' ).autocomplete( { 4 source: function( request, response ) { 5 var form_data = $('form').serialize(); 6 $.post( 7 WPorgAutocomplete.search_url, 8 { 9 action: WPorgAutocomplete.search_action, 10 dataType: "jsonp", 11 data: form_data, 12 }, 13 function( result ) { 14 response( JSON.parse( result ) ); 15 } 16 ); 17 }, 18 delay: 500, 19 minLength: 3 20 } ) ; 21 } ); 22 } )( jQuery ); -
searchform.php
37 37 <div> 38 38 <label> 39 39 <span class="screen-reader-text"><?php _ex( 'Search for:', 'label', 'wporg' ); ?></span> 40 <input type="text" class="search-field" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder', 'wporg' ); ?>" value="<?php echo esc_attr( get_search_query() ); ?>" name="s">40 <input type="text" class="search-field" id="s" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder', 'wporg' ); ?>" value="<?php echo esc_attr( get_search_query() ); ?>" name="s"> 41 41 </label> 42 42 <input type="submit" class="shiny-blue search-submit" value="<?php echo esc_attr_x( 'Search', 'submit button', 'wporg' ); ?>"> 43 43 </div>