To change the default search URL slug in WordPress, you can use the rewrite_rules_array filter hook to modify the rewrite rules. By adding a custom rule, you can change the URL slug for the search results page.
Here’s an example code snippet that demonstrates how to change the default search URL slug to “find”:
function wpsnippets_change_search_url_slug( $rules ) {
$new_rules = array(
'find/?$' => 'index.php?s=' . urlencode( get_query_var( 's' ) ),
);
return $new_rules + $rules;
}
add_filter( 'rewrite_rules_array', 'wpsnippets_change_search_url_slug' );
In this example, we’re using the rewrite_rules_array filter hook to modify the rewrite rules. The wpsnippets_change_search_url_slug function takes the existing rules as a parameter and adds a new rule to it. The new rule maps the “find” URL slug to the search results page, with the search query passed as a parameter.
After adding this code snippet to your theme’s functions.php file or a custom plugin, you’ll need to flush the rewrite rules for the changes to take effect. You can do this by visiting the “Settings” > “Permalinks” page in your WordPress admin area and simply clicking the “Save Changes” button.
Once the code is implemented and the rewrite rules are flushed, the default search URL slug will be changed to “find”. For example, the search results page for the query “example” will be accessible at example.com/find/?s=example.
This code snippet can be useful if you want to customize the search URL slug to better fit your website’s branding or SEO strategy.
Examples
Example 1: Changing the default search URL slug using the rewrite_rules_array filter
This example demonstrates how to change the default search URL slug from /search/ to /find/ using the rewrite_rules_array filter.
function wpsnippets_change_search_url_slug( $rules ) {
$new_rules = array(
'find/?$' => 'index.php?s=' . urlencode( get_query_var( 's' ) ),
);
return $new_rules + $rules;
}
add_filter( 'rewrite_rules_array', 'wpsnippets_change_search_url_slug' );
Explanation: The rewrite_rules_array filter allows us to modify the rewrite rules used by WordPress for generating pretty permalinks. In this code example, we define a new rule that maps the /find/ URL to the search query. The get_query_var('s') function retrieves the search query, and urlencode() ensures proper encoding of the query string. Finally, we add our new rule to the existing rules using the + operator.
Example 2: Changing the default search URL slug using the rewrite_rules_array and query_vars filters
This example demonstrates an alternative approach to change the default search URL slug by combining the rewrite_rules_array and query_vars filters.
function wpsnippets_change_search_url_slug( $rules ) {
$new_rules = array(
'find/?$' => 'index.php?s=' . urlencode( get_query_var( 's' ) ),
);
return $new_rules + $rules;
}
add_filter( 'rewrite_rules_array', 'wpsnippets_change_search_url_slug' );
function wpsnippets_add_search_query_var( $query_vars ) {
$query_vars[] = 's';
return $query_vars;
}
add_filter( 'query_vars', 'wpsnippets_add_search_query_var' );
Explanation: In this code example, we use the rewrite_rules_array filter to define the new search URL slug rule, just like in the previous example. Additionally, we use the query_vars filter to add the s query variable to the list of recognized query variables. This ensures that WordPress correctly identifies the s parameter as the search query.
Example 3: Changing the default search URL slug using the rewrite_rules_array filter and flushing rewrite rules
This example demonstrates how to change the default search URL slug and ensure the changes take effect by flushing the rewrite rules.
function wpsnippets_change_search_url_slug( $rules ) {
$new_rules = array(
'find/?$' => 'index.php?s=' . urlencode( get_query_var( 's' ) ),
);
return $new_rules + $rules;
}
add_filter( 'rewrite_rules_array', 'wpsnippets_change_search_url_slug' );
function wpsnippets_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'init', 'wpsnippets_flush_rewrite_rules' );
Explanation: In this code example, we follow the same approach as in the first example to change the default search URL slug. However, we also add an init action hook to trigger the flush_rewrite_rules() function, which regenerates the rewrite rules and updates the .htaccess file. This ensures that the changes to the search URL slug take effect immediately.
