Add the following code to your functions.php to replace the default WordPress search parameter (i.e., …pagename/?s=search_phrase…).
-
-
// Allow WordPress to access "search" in the query string
-
function ds_whitelist_new_search_parameter( $allowed_query_vars ) {
-
$allowed_query_vars[] = 'search';
-
return $allowed_query_vars;
-
}
-
add_filter('query_vars', 'ds_whitelist_new_search_parameter' );
-
-
-
// populate s parameter with value of search
-
function ds_swap_search_parameter($query_string) {
-
-
$query_string_array = array();
-
-
// convert the query string to an array
-
parse_str($query_string, $query_string_array);
-
-
// if "search" is in the query string
-
if(isset($query_string_array['search'])){
-
$query_string_array['s'] = $query_string_array['search']; // replace "s" with value of "search"
-
unset($query_string_array['search']); // delete "search" from query string
-
}
-
-
return http_build_query($query_string_array, '', '&'); // Return our modified query variables
-
}
-
add_filter('query_string', 'ds_swap_search_parameter');
My example uses “search”. So instead of example.com/?s=search+phrase my site uses example.com/?search=search+phrase (both are recognized).
This is handy if you have another snippet on your page that also wants to use the ‘s’ parameter (like the embed code from sharefile.com). To replace other WordPress query vars (like p or order) replace ‘s’ in $query_string_array['s'] (near line 21) with the query var you’d like to replace (see http://codex.wordpress.org/WordPress_Query_Vars for a partial list of query variables). Just make sure the new query variable name doesn’t conflict with any of the built-in ones.
Once you’ve added this code, you’ll also need to update any search forms you have with the new variable name (in your theme directory searchform.php).

