1

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.

5
  • 1
    You could probably use the blog_id as a conditional inside your function. So, if blog_id is 1, redirect to /search, if blog_id is 2, redirect to /suchen, etc.
    – WebElaine
    Commented Nov 20, 2019 at 16:20
  • @WebElaine I have edited the question to include what I did try and didn't work. Kind of works on the same logic but it didnt work. (apologies I should have included it in the original post)
    – noelmcg
    Commented Nov 20, 2019 at 16:33
  • 1
    In your function $option isn't defined, so it looks like you would always hit the else case. Try changing ` $locstr = $option['htprtswrhs_o_locale'];` to ` $locstr = $get_option('htprtswrhs_o_locale');`
    – WebElaine
    Commented Nov 20, 2019 at 17:07
  • @WebElaine sorry I pulled that from a working site, yeah option is defined I have edited the function accordingly. The function does work and changes the sites url as per the logic, but they return 404s
    – noelmcg
    Commented Nov 20, 2019 at 17:18
  • Have you set up pages at those URLs? By default WP search ends up calling your home URL with a query string. If you want search to be on other pages you have to create those pages.
    – WebElaine
    Commented Nov 20, 2019 at 19:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.