How to Redirect Empty Searches to Root in WordPress

Home » Snippets » How to Redirect Empty Searches to Root in WordPress

Created with:



Creator: WPTurbo Team

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project

Have you ever observed that often when a user performs an empty search on your WordPress website, they’re redirected to a page with no results or even an error? Would you rather redirect these users back to the homepage instead? You’re in luck! In this article, we’re going to guide you on how to redirect all empty searches back to the root or homepage of your WordPress site, ensuring a seamless user experience despite any missteps.

					function wpturbo_redirect_empty_search_to_root($query_vars) {
    if (isset($_GET['s']) && empty($_GET['s'])) {
        $query_vars['s'] = " ";
    return $query_vars;
add_filter('request', 'wpturbo_redirect_empty_search_to_root');

At its core, this code provides a solution to redirect searches where no search term has been entered back to the website’s root, or home page – essentially a mechanism to gracefully handle empty searches.

Let’s go through how this works:

The function wpturbo_redirect_empty_search_to_root($query_vars). is defined. In this function, $query_vars is a parameter, that passes a set of variables, which consists of details about the search term.

Inside the function, the conditional if statement is used to check two conditions:

1) If a search has been attempted at all. This is evaluated by isset($_GET['s']), which checks whether a GET request for a search (‘s’) has been executed.

2) If the search term entered was empty. This is evaluated by empty($_GET['s']), which checks if the GET for ‘s’ is empty i.e. no search terms have been entered.

These two checks ensure that the function only progresses if a search has been conducted, but with an empty search term.

If both conditions are satisfied, $query_vars['s'] = " "; assigns a single space as the search term. This is important because WordPress does not usually allow search terms to be empty, this workaround replicates an empty search without triggering a WordPress error.

In essence, this line is letting WordPress know that a search has been conducted, but with a ‘blank’ search term, thereby circumventing any errors that may occur from an empty search term.

Finally, the function return $query_vars; returns the search’s query variables, now with the altered ‘blank’ search term if the original term was empty. This function now effectively handles searches that contain empty search terms.

The function wpturbo_redirect_empty_search_to_root is then added as a filter using add_filter function. The filter is applied to the ‘request’, meaning that it will run whenever a request is made i.e., when a search is conducted. The result of which is, any empty search term will be replaced with a blank indicated by " " and redirected back to the root page.

This snippet, overall, provides a simple and effective way to prevent errors on your WordPress site when an empty search is conducted.

Register an account to save your snippets or go Pro to get more features.