Changing Search Query Parameter in WordPress

October 19, 2010 Posted in: Side Notes by mkozlov
You may wonder why some would do such thing? Well, most obvious case is integration with existing website. Some times "s" parameter is already used and/or required to have function different from search. This where you have to alter default behavior of WordPress.

Future Problems

First let's look at drawbacks. Apart from altering structure of well functioning application, the biggest problem that will rise in future will be updates. Every time new update is out, you would have to repeat this procedure and then merge new code into production environment.

Steps

Nonetheless we have to do it, so let's get started.

Step 1: Setup

Firstly, you would need software that allows to search and replace on entire application folder. Most obvious example for Windows would be Aptana. If you have good software examples for other OSs, be sure to share it in comments.

Step 2: Search/Replace

Now it is time to search and replace most obvious cases: $_GET['s'], $_REQUEST['s'], and $_POST['s']. I have to note that last one happens only once or twice, so you may want to hit later. You can go ahead and replace those with $_GET['search'], $_REQUEST['search'], and $_POST['search'] accordingly.

Step 3: Search/Replace More

As you probably guessed getting parameter form request is not the only case where you may need to alter WordPress's code. Second thing to look for is 's'. You will see that number of results will be much greater than you expected. Now it is important to pay attention. Searching for 's' will return files that have nothing to do with search functionality. Most of JS code has nothing to do with search, so you can skip it for now. Look at PHP files that lay in wp-includes and wp-admin folders, but keep mind that some times 's' may mean seconds in time formating of just be appended to the end of word.
	$edit_others_cap = 'edit_others_' . $post_type_cap . 's';
	$read_private_cap = 'read_private_' . $post_type_cap . 's';

Step 4: Forget about ?s=

Inside wp-includesclasses.php you'll find that $public_query_vars keeps records of common query variables, so need to tell WordPress to forget ?s.
var $public_query_vars = array('m', 'p', 'posts', 'w', 'cat', 'withcomments', 'withoutcomments', 'search', 'exact', 'sentence', 'debug', 'calendar', 'page', 'paged', 'more', 'tb', 'pb', 'author', 'order', 'orderby', 'year', 'monthnum', 'day', 'hour', 'minute', 'second', 'name', 'category_name', 'tag', 'feed', 'author_name', 'static', 'pagename', 'page_id', 'error', 'comments_popup', 'attachment', 'attachment_id', 'subpost', 'subpost_id', 'preview', 'robots', 'taxonomy', 'term', 'cpage', 'post_type');

Step 5:JS Code

The only instance of JavaScript I could think of was function that keeps focus on search field in 404.php page.
document.getElementById('s') && document.getElementById('s').focus();
becomes:
document.getElementById('search') && document.getElementById('search').focus();

Done

Well we did it. Feel free to share any thoughts in comments.
 

2 Responses to Changing Search Query Parameter in WordPress

  1. mikhail says:

    Here is another way to do it less hacky way on the site with permalinks turned on: http://wpengineer.com/2258/change-the-search-url-of-wordpress/

  2. Jeaydeepa says:

    Thank you, this is help full for me thank you once again.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>