Created
June 24, 2015 13:27
-
-
Save georgiecel/a611826178b1414d28d4 to your computer and use it in GitHub Desktop.
friendly URL for WordPress search (/search/term/) – insert into functions.php
This file contains hidden or 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
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(); | |
} | |
} | |
if ( current_theme_supports('nice-search') ) { | |
add_action( 'template_redirect', 'roots_nice_search_redirect' ); | |
} | |
function change_search_url_rewrite() { | |
if ( is_search() && ! empty( $_GET['s'] ) ) { | |
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) ); | |
exit(); | |
} | |
} | |
add_action( 'template_redirect', 'change_search_url_rewrite' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment