Nice Search Slug Translation

Hi there,

I was wundering if it’s possible to have a custom nice search slug?
When I try to change the search_base in the roots_nice_search_redirect function in lib/cleanup.php into something like this:

$GLOBALS['wp_rewrite']->search_base = __('zoeken', 'mytextdomain');
$search_base = $wp_rewrite->search_base;

and flush my permalinks afterwards, nice search stops working and gives me a 404 page.
I also tried changing these lines with the same result:

$search_base = 'zoeken';

and

wp_redirect(home_url("/zoeken/" . urlencode(get_query_var('s'))));

Is there something else I need to change somewhere?

Thanks!

I wouldn’t do this.

The change you made to $wp_rewrite->search_base should be done on init not during the template_redirect.

You would also have to manually add each of the possible options for the search slug to the rewrite rules, or the redirect will 404.

Thanks for your reply!
I got it working by adding the search rewrite rules and changing the $wp_rewrite->search_base in a seperate on init function.

This is my code:

/**
 * Rewrites search base slug from 'search' to 'zoeken'
 */
function roots_nice_search_rewrite() {
	$GLOBALS['wp_rewrite']->search_base = __('zoeken', 'mytextdomain');
}
add_action( 'init', 'roots_nice_search_rewrite' );

/**
 * Rewrites rules for search slug
 */
function my_search_rewrite_rules($rules) {
	$search_base = $wp_rewrite->search_base;
	foreach($rules as $rule => $target){
		$new_rule = str_replace('search', $search_base, $rule);
		unset($rules[$rule]);
		$rules[$new_rule] = $target;
	}
	return $rules;
}
add_filter('search_rewrite_rules', 'my_search_rewrite_rules');

I don’t think that will work unless you flush the rewrite rules on every page load.
Try changing the language and searching again.

It does work for me. My search url is now: http://mydomain.com/zoeken/test

Which returns the searchresults perfectly for ‘test’, even with working pagination.

Because ‘zoeken’ is translatable, anyone using your site in a different language will get a 404 because the rewrite rules will not support the translated term until the rewrite rules are flushed.

So this will work fine long if everyone is using the same language or you use ‘zoeken’ as a static (i.e. non-translatable) string.

Okay, got it. Thank you!
For now the site is only in one language which wasn’t English, so search was the only non translated slug left.