Thursday, March 11, 2010 |
||
Thanks to some under-the-hood black magic, newer versions of WordPress support a streamlined permalink structure for search URLs. (In some corners, these are known as “cruft-free” URLs.)
WordPress-powered URLs ending in /search/foo (where “foo” is your search term) now work the same way as the old-school format of index.php?s=foo. They’re cleaner, more intuitive, and darned easier on the eyes.
The only problem? The search form included in most templates produces old-school URLs instead of the newer ones, and the various ways of recoding the search form (see last year’s discussion on the wp-hackers mailing list) seem more trouble than they’re worth.
Thankfully, there’s a simple, elegant workaround using the WordPress WP_Query class. Just add the code below to the very top of your header.php template and say goodbye to ugly search URLs.
<?php
if ((is_search()) && ($_GET['s'])) {
wp_redirect(get_bloginfo('url')."/search/".get_query_var('s'));
} //if
?>
As usual, standard disclaimers apply.