Skip to content

Instantly share code, notes, and snippets.

@sporkman
Created January 5, 2016 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sporkman/922e64e2435188ec90a7 to your computer and use it in GitHub Desktop.
Save sporkman/922e64e2435188ec90a7 to your computer and use it in GitHub Desktop.
/**
* Redirects search results from /?s=query to /search/query/, converts %20 to +
*
* @link http://txfx.net/wordpress-plugins/nice-search/
*/
function roots_nice_search_redirect() {
global $wp_rewrite;
if (!isset($wp_rewrite) || !is_object($wp_rewrite) || !$wp_rewrite->using_permalinks()) {
return;
}
$search_base = $wp_rewrite->search_base;
if (is_search() && !is_admin() && strpos($_SERVER['REQUEST_URI'], "/{$search_base}/") === false) {
wp_redirect(home_url("/{$search_base}/" . urlencode(get_query_var('s'))));
exit();
}
}
add_action('template_redirect', 'roots_nice_search_redirect');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment