Building a Custom Search Functionality for Your Plugin

Posted on 18th June 2023

As a WordPress plugin developer, you may find yourself in a situation where you need to add a custom search functionality to your plugin. This could be for a number of reasons, but the most common one is probably because you want to allow your users to search your plugin’s data.

Fortunately, WordPress makes it relatively easy to add a custom search to your plugin. In this article, we’re going to show you how to do just that.

Creating a Custom Search Form

The first thing you need to do is to create a custom search form. This can be done by creating a new file in your plugin’s directory and adding the following code to it:

<form action="” method=”get”>

<input type="text" name="s" id="s" value="” />

This search form will allow your users to search your plugin’s data by entering a search term into the text field and hitting the “Search” button.

Adding a Custom Search Function

Next, you need to add a custom search function to your plugin. This function will be responsible for searching your plugin’s data and returning the results.

To do this, you’ll need to add the following code to your plugin:

function my_plugin_search( $search_term, $post_type = ‘my_plugin_post_type’ ) {
global $wpdb;

$search_term = esc_sql( $search_term );

$query = $wpdb->prepare(
“SELECT * FROM $wpdb->posts
WHERE post_type = %s
AND post_title LIKE %s”,
$post_type,
“%$search_term%”
);

$posts = $wpdb->get_results( $query );

return $posts;
}

This function will search your plugin’s data for the specified search term and return the results.

Adding the Search Form to Your Plugin

Finally, you need to add the search form to your plugin. This can be done by adding the following code to your plugin:

function my_plugin_search_form() {
$search_form = my_plugin_search_form();
echo $search_form;
}

add_action( ‘my_plugin_search_form’, ‘my_plugin_search_form’ );

This code will add the search form to your plugin.

Conclusion

In this article, we’ve shown you how to add a custom search functionality to your plugin. We’ve also shown you how to create a custom search form and how to add a custom search function to your plugin.

Assuming you have a plugin that creates a custom post type for “books”, the following code would create a search form to search for books by title:

<form role="search" method="get" class="search-form" action="”>

This search form can be added to any page on your website. When the user submits the form, they will be taken to the search results page, which will display all books that match the search query.

If you want to limit the search results to only books, you can use the following code:

$s,
‘post_type’ => ‘book’
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// display title and excerpt for each book
}
} else {
// no books found
}
wp_reset_postdata();
?>

This code will run a new query using the WP_Query class. The search string ( $s ) is passed to the query, as well as the post type ( book ). This will return all books that match the search query.

You can also use this technique to create a search form for any other custom post type. Simply change the post_type parameter in the args array to the name of your custom post type.

Assuming you have a basic understanding of PHP (if not, check out our series “Learn PHP in 15 minutes”), building a custom search function for your plugin is not too difficult. Let’s take a look at how to do it.

There are two parts to this: first, we need to add a search form to our plugin’s UI, and second, we need to write the code that actually searches our plugin’s data and displays the results.

Adding the search form is simple: we just need to add a text input and a submit button to our plugin’s UI. The code for this might look something like this:

<form action="” method=”GET”>
<input type="text" name="s" value="” placeholder=”Search…” />

The code above will add a basic search form to our plugin’s UI. The form will POST to the current page, and the search term will be passed in the ‘s’ parameter.

Now that we have our search form, we need to write the code that will actually search our plugin’s data and display the results.

The first thing we need to do is check if the ‘s’ parameter is set in the $_GET array. If it is, we know that the user has submitted the search form and we can start searching our plugin’s data:

if (isset($_GET[‘s’])) {
// code to search plugin’s data and display results
}

Next, we need to write the code that will search our plugin’s data. This will be specific to your plugin, so I can’t give you exact code to use here. But the general idea is to loop through your plugin’s data (whether it’s stored in a database, in files, or elsewhere), and check if the search term is present. If it is, add that data to an array (or object) that we can later loop through to display the results.

Once we have our results, we can loop through them and display them to the user:

if (isset($_GET[‘s’])) {
// code to search plugin’s data and store results in $results

if (!empty($results)) {
// loop through results and display them
foreach ($results as $result) {
echo $result;
}
} else {
echo ‘No results found.’;
}
}

And that’s all there is to it! With just a few lines of code, you can add a powerful search feature to your plugin.