As per the title, how would you change the search url for different sites in a multisite network. eg:
testsite.co.uk/search
testsite.de/suchen
testsite.fr/recherche
I can change the search term for one, and all, sites using:
function change_search_url_rewrite() {
if ( is_search() && isset($_GET['s']) ) {
$str = get_query_var( 's' );
$str = sanitize_text_field($str);
//replace slash with html entity
$str = str_replace('/', '∕', $str);
$str = preg_replace('/\s+/', ' ', $str );
if(empty($str)){
$str = " ";
}
wp_redirect( home_url( "/search/" ) . urlencode( $str ) );
exit();
}
}
add_action( 'template_redirect', 'change_search_url_rewrite',1 );
Having the search term change on a per site basis does not work though.
Through the use of an option field in a mu plugin, I couldnt get the term to conditionally change, as per:
function change_search_url_rewrite() {
if ( is_search() && isset($_GET['s']) ) {
$option = get_option('test_options');
// get theme option
$locstr = $option['test_o_locale'];
if($locstr === 'fr'){
$strurl = "recherche";
}elseif($locstr === 'de'){
$strurl = "suchen";
}else{
$strurl = "search";
}
$str = get_query_var( 's' );
$str = sanitize_text_field($str);
//replace slash with html entity
$str = str_replace('/', '∕', $str);
$str = preg_replace('/\s+/', ' ', $str );
if(empty($str)){
$str = " ";
}
wp_redirect( home_url( "/".$strurl."/" ) . urlencode( $str ) );
exit();
}
}
add_action( 'template_redirect', 'change_search_url_rewrite',1 );
The function does work and changes the sites url as per the logic, but they return 404s.
Could anyone please advise, I am assuming I need to address this through htaccesss? But not 100% sure if that would be the right way to go.
Any help appreciated, cheers.
blog_id
as a conditional inside your function. So, ifblog_id
is 1, redirect to/search
, ifblog_id
is 2, redirect to/suchen
, etc.$option
isn't defined, so it looks like you would always hit theelse
case. Try changing ` $locstr = $option['htprtswrhs_o_locale'];` to ` $locstr = $get_option('htprtswrhs_o_locale');`