'themeidol-ajaxsearch-widget', 'description' => __( 'Search form with AJAX results', 'themeidol-all-widget' ) ) ); add_action( 'init', array( $this, 'themeidol_ajaxsearch_register_script' ) ) ; // only load scripts when an instance is active if ( is_active_widget( false, false, $this->id_base ) && !is_admin() ) add_action( 'wp_footer', array( $this, 'themeidolasw_print_script' ) ); add_action( 'wp_ajax_themeidolasw', array( $this, 'themeidolasw_ajax' ) ); add_action( 'wp_ajax_nopriv_themeidolasw', array( $this, 'themeidolasw_ajax' ) ); } /** * widget * * Output the widget * * @return void */ function widget( $args, $instance ) { extract( $args, EXTR_SKIP ); $title = empty( $instance['title'] ) ? '' : apply_filters( 'widget_title', esc_attr($instance['title']) ); $username = empty( $instance['username'] ) ? '' : esc_attr($instance['username']); $limit = empty( $instance['number'] ) ? 10 : esc_attr($instance['number']); echo $before_widget; if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }; do_action( 'wpasw_before_widget', $instance ); get_search_form( true ); ?>
__( 'Search', 'themeidol-all-widget' ), 'number' => 10 ) ); $title = esc_attr( $instance['title'] ); $number = absint( $instance['number'] ); ?>

add_query_arg( array( 'action' => 'themeidolasw', '_wpnonce' => wp_create_nonce( 'themeidolasw' ) ), untrailingslashit( set_url_scheme( admin_url( 'admin-ajax.php' ) ) ) ), ) ); } /** * wpasw_print_script * * Output JS only when widget in use on page * * @return void */ function themeidolasw_print_script() { wp_print_scripts( 'themeidolasw' ); } /** * ajax * * Handle the search request * * @return string */ function themeidolasw_ajax() { // verify the nonce if ( wp_verify_nonce( $_REQUEST['_wpnonce'], 'themeidolasw' ) ) { // clean up the query $s = trim( stripslashes( $_POST['s'] ) ); // cancel if no search term is set if ( !$s ) die(); // get the settings for this widget instance $instance = $this->get_settings(); if ( array_key_exists( $this->number, $instance ) ) { $instance = $instance[$this->number]; } do_action( 'themeidolasw_before_results' ); // set the query limit $limit = empty( $instance['number'] ) ? 10: $instance['number']; $query_args = apply_filters( 'themeidolasw_query', array( 's' => $s, 'post_status' => 'publish', 'posts_per_page' => $limit ), $s, $limit ); $search = new WP_Query( $query_args ); if ( $search->have_posts() ) : ?>