Pretty WordPress Search URLS Without Plugins

Pretty WordPress Search URLS Without Plugins

This tutorial will show how to easily transform your WordPress search urls into a more readable and frieindly rewrittend structure instead of the default url which contains ?s= and without using any additional WordPress plugins.

With a default installation of WordPress and when url rewriting is enabled the search url’s remain unaffected. The default WordPress search query for wordpress plugins for example outputs the url like below.

http://wpshock.com/?s=wordpress+plugins&submit=Search

What this tutorial will do is change the output of your WordPress search urls usuing the WordPress wp_redirect function to look like the example below which as you can see is a much cleaner url output.

http://wpshock.com/search/wordpress+plugins

Open your themes functions.php file and paste the code below into it, save that file then go check out your new search urls.

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

Let us know if you have any improvments or have any errors with this code snippet in the comments.

Tagged: , , ,

Author:  | Website: http://stuartduff.com/ | Twitter: Follow Stuart

Stuart Duff is the founder of WPShock and works at WooThemes as a support ninja. In his spare time you can find Stuart Developing plugins and themes for WordPress, discussing WordPress or drinking tea.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>