Paulund
2012-04-24 #wordpress

Redirect WordPress Searches

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.


https://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' );