How to Rewrite the Search Results Slug for a Specific Search Term in WordPress

Home » Snippets » How to Rewrite the Search Results Slug for a Specific Search Term in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you noticed the ‘s=’ component in your WordPress site’s search URL? Did you ever wish to change that to something more user and SEO friendly? If so, you’ve come to the right place. This guide will shed light on how to rewrite the search results slug using your desired search term, enhancing your website’s URL structure, and contributing to better performance in search engine results.

					function wpturbo_rewrite_search_slug() {
    if (is_search() && !empty($_GET['s'])) {
        wp_redirect(home_url("/search/") . urlencode(get_query_var('s')));
        exit();
    }   
}
add_action('template_redirect', 'wpturbo_rewrite_search_slug');
				

The provided code snippet is all about customizing or rewriting the search results slug in a WordPress website for more SEO benefits and user-friendly URLs. It’s a function named wpturbo_rewrite_search_slug() which is hooked into WordPress’s template_redirect action.

Let’s break down what the function does:

    if (is_search() && !empty($_GET['s'])) {
        wp_redirect(home_url("/search/") . urlencode(get_query_var('s')));
        exit();
    }

The function first checks if the current request is a search query. This is done using the is_search() WordPress function, which returns true if a search result page is being displayed. Additionally, it checks whether the ‘s’ query variable (the search term) is not empty. This is accomplished by checking $_GET['s'].

If both conditions are fulfilled (meaning, we’re on a search page and a search term has been entered), the function will redirect to a newly formatted URL.

That new URL is formed by concatenating the home URL (retrieved by the home_url() function) with "/search/" and the URL-encoded search term. The urlencode() function is used to ensure that the search term is appropriately formatted for use in a URL (it encodes space as ‘+’, for instance). The search term itself is fetched with get_query_var('s').

After this redirection is finished, exit() is called to halt further script execution.

The last line is where the function is hooked into WordPress using add_action().

add_action('template_redirect', 'wpturbo_rewrite_search_slug');

The template_redirect action hook fires just after WordPress has determined the content type to display, but before it begins to generate the output. This is the best time for our function to step in and apply the rewrite rule.

So, instead of default ‘s’ parameter WordPress uses, this function turns search URLs from ‘website.com/?s=query’ into the cleaner ‘website.com/search/query’.

This not only makes the URL look more eye-catching and easy to comprehend for users, it is also considered good for SEO purposes as it contains the keyword in the slug. Furthermore, these rewritten URLs also align better with popular search engine patterns, enabling crawlers to understand the content of the pages more accurately.

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