-
-
Save miziomon/2a41da4cc962817b1304 to your computer and use it in GitHub Desktop.
Redirects search results from /?s=query to /your-keyword/query/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Redirects search results from /?s=query to /search/query/, converts %20 to + | |
* | |
* @link https://wordpress.org/plugins/nice-search/ | |
* | |
*/ | |
add_action( 'template_redirect', function () { | |
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(); | |
} | |
}); | |
/** | |
* Changhe the search_base add rewrite_rule | |
* Only if != search | |
*/ | |
add_action( 'init', function () { | |
$GLOBALS['wp_rewrite']->search_base = 'search-results'; | |
add_rewrite_rule('^' . $GLOBALS['wp_rewrite']->search_base . '/([^/]+)/?$', 'index.php?s=$matches[1]', "top"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment