How to Implement AJAX Search in WordPress Plugin
Posted on 17th June 2023
WordPress is a great platform for developing plugins. One popular feature that you may want to add to your plugin is AJAX search. AJAX search allows your users to search your site without having to reload the page. In this article, we will show you how to implement AJAX search in WordPress plugin.
Step 1: Create the AJAX Handler
The first thing you need to do is to create an AJAX handler. This is a PHP function that will process the AJAX request and return the results.
The AJAX handler function must be hooked into the wp_ajax_nopriv and wp_ajax action hooks. This is because the AJAX request will be made by a user who is not logged in (nopriv) and a user who is logged in (ajax).
Step 2: Process the AJAX Request
Next, you need to process the AJAX request and return the results.
First, we get the search term from the AJAX request. Then we use the search_posts() function to perform the search. This is a custom function that you need to create. It will search the posts and return the results. Finally, we use the wp_send_json() function to return the search results.
Step 3: Create the search_posts() Function
The search_posts() function is where you will perform the search. You can use the WordPress search functions or write your own custom search function. Once the search is performed, you need to return the search results.
Step 4: Enqueue the AJAX Script
Next, you need to enqueue the AJAX script. This script will make the AJAX request and process the results.
admin_url( ‘admin-ajax.php’ )
));
}
add_action( ‘wp_enqueue_scripts’, ‘ajax_search_scripts’ );
?>
First, we use the wp_enqueue_script() function to enqueue the AJAX script. Then we use the wp_localize_script() function to pass the AJAX URL to the script.
Step 5: Create the AJAX Script
Finally, you need to create the AJAX script.
jQuery(document).ready(function($){
$(‘#search-form’).submit(function(e){
e.preventDefault();
var search_term = $(‘#search-term’).val();
$.ajax({
type: ‘POST’,
url: ajax_search_params.ajax_url,
data: {
action: ‘ajax_search’,
search_term: search_term
},
success: function(results){
console.log(results);
}
});
});
});
This script will get the search term from the search form and make an AJAX request. The AJAX request will be processed by the AJAX handler we created earlier. Finally, the search results will be logged to the browser console.
Step 6: Create the Search Form
Now that we have everything in place, we can create the search form.
This is a very basic search form. You can style it however you want.
Step 7: Testing the AJAX Search
Now that we have everything in place, we can test the AJAX search. Enter a search term in the search form and submit it. You should see the search results in the browser console.
That’s it! You have successfully implemented AJAX search in your WordPress plugin.
In this article, we will show you how to implement AJAX search in WordPress plugin. This can be done by using the WordPress AJAX API.
The first thing you need to do is to add the following code in your plugin file:
$_GET[‘term’],
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$results = array();
while ( $the_query->have_posts() ) {
$the_query->the_post();
$results[] = array(
‘title’ => get_the_title(),
‘permalink’ => get_permalink(),
);
}
wp_reset_postdata();
wp_send_json( $results );
}
wp_die();
}
This code adds two new action hooks: wp_ajax_nopriv_my_search and wp_ajax_my_search. The first one is for users that are not logged in, and the second one is for logged in users.
The my_search() function is where the AJAX search is executed. It first gets the search term from the $_GET[‘term’] variable.
Next, it sets up a new WP_Query query with the search term as the s (search) parameter.
If there are any results, they are looped through and added to an array. This array is then converted to JSON and sent back to the browser.
The wp_die() function is important, as it prevents WordPress from executing the rest of the code after the AJAX call has been made.
Finally, you need to add the following code to your plugin file:
<?php
add_action( 'wp_enqueue_scripts', 'my_scripts' );
function my_scripts() {
wp_enqueue_script( 'my-script', plugins_url( 'my-script.js', __FILE__ ), array( 'jquery' ), '1.0', true );
wp_localize_script( 'my-script', 'my_ajax_url', admin_url( 'admin-ajax.php' ) );
}
The wp_enqueue_scripts action hook is used to load the my-script.js file. This file contains the JavaScript code that makes the AJAX call to the my_search() function.
The wp_localize_script() function is used to pass the admin_url('admin-ajax.php') value to the my_script.js file. This is the URL of the WordPress AJAX API.
And that's it! You have now successfully implemented AJAX search in your WordPress plugin.