Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WP - Pretty urls for search results
/**
* Pretty urls for search results
*/
function 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', 'nice_search_redirect');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment