您可以使用template_redirect. 这里有一个简单的重定向函数。
add_action( \'template_redirect\', \'se219663_template_redirect\' );
function se219663_template_redirect()
{
global $wp_rewrite;
if ( is_search() && ! empty ( $_GET[\'s\'] ) )
{
$s = sanitize_text_field( $_GET[\'s\'] ); // or get_query_var( \'s\' )
$location = \'/\';
$location .= trailingslashit( $wp_rewrite->search_base );
$location .= user_trailingslashit( urlencode( $s ) );
$location = home_url( $location );
wp_safe_redirect( $location, 301 );
exit;
}
}
没有搜索查询的规则,如下所示(不要忘记转到永久链接设置刷新规则):
add_filter( \'search_rewrite_rules\', \'se219663_search_rewrite_rules\', 10, 1 );
function se219663_search_rewrite_rules( $rewrite )
{
global $wp_rewrite;
$rules = array(
$wp_rewrite->search_base . \'/?$\' => \'index.php?s=\',
);
$rewrite = $rewrite + $rules;
return $rewrite;
}
并用空搜索查询重定向,只需使用isset,根据上面的代码修改即可。
add_action( \'template_redirect\', \'se219663_template_redirect\' );
function se219663_template_redirect()
{
global $wp_rewrite;
if ( is_search() && isset ( $_GET[\'s\'] ) )
{
$s = sanitize_text_field( $_GET[\'s\'] ); // or get_query_var( \'s\' )
$location = \'/\';
$location .= trailingslashit( $wp_rewrite->search_base );
$location .= ( ! empty ( $s ) ) ? user_trailingslashit( urlencode( $s ) ) : urlencode( $s );
$location = home_url( $location );
wp_safe_redirect( $location, 301 );
exit;
}
}