in WordPress

Redirect WordPress Searches

Wordpress WordPress comes with it's own in-built search functionality which can search the title and content of all your posts on your Wordpress site. To perform a search on your WordPress site all you have to do is create a search form which posts a GET parameter s to the home page of your WordPress site.


http://www.paulund.co.uk/?s=wordpress

WordPress will then search for all posts which have the word WordPress in them. But the URL of the search page is just domain/?s=[search query]. This is a messy URL, which you can change to be /search/wordpress, by using the following code snippet. Add the following to your functions.php file.


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

Upgrade to access all content on Paulund

Members unlock all tutorials and snippets

Access to all downloadable content

Access to code examples before others

Sign Up Now

Already a member? Login here

Subscribe To The Weekly Newsletter

Get weekly updates to your email