PHP’s urlencode()
function replaces spaces with +
s, so that’s why that’s happening for you.
WordPress provides the sanitize_title()
function, which is used (among other things) to generate a post slug from the post’s title.
/** * Change search page slug. */
function wp_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/tag/" ) . sanitize_title( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wp_change_search_url' );