admin管理员组

文章数量:1406217

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.

本文标签: htaccessChange Search url slug for each site in a multisite network