How to customise the search box in Drupal 6
By default, Drupal allows you to include a search box directly into your theme. In most themes, when enabled, this search box will show up in the primary navigation bar as an input box labeled "Search this site", with a submit button labeled "Search". But what if you want to alter or completely hide these labels, or even add a new class to the input box?
How to do it
There have been loads of suggestions about how to do this on drupal.org, including altering the core Drupal source (not a good idea!), using a string replace function in a custom template file or using the String overrides module. But, the best suggestion came from this post and is the most Drupal friendly way of doing it.
Use Drupal 6's preprocess function in your theme's template.php file to modify the template variables before they are passed into the template files.
/**
* Override or insert PHPTemplate variables into the search_theme_form template.
*
* @param $vars
* A sequential array of variables to pass to the theme template.
* @param $hook
* The name of the theme function being called (not used in this case.)
*/
function mytheme_preprocess_search_theme_form(&$vars, $hook) {
// Modify elements of the search form
$vars['form']['search_theme_form']['#title'] = t('Search mysite.com');
// Set a default value for the search box
$vars['form']['search_theme_form']['#value'] = t('Search');
// Add a custom class to the search box
$vars['form']['search_theme_form']['#attributes'] = array('class' => t('cleardefault'));
// Change the text on the submit button
$vars['form']['submit']['#value'] = t('Go');
// Rebuild the rendered version (search form only, rest remains unchanged)
unset($vars['form']['search_theme_form']['#printed']);
$vars['search']['search_theme_form'] = drupal_render($vars['form']['search_theme_form']);
// Rebuild the rendered version (submit button, rest remains unchanged)
unset($vars['form']['submit']['#printed']);
$vars['search']['submit'] = drupal_render($vars['form']['submit']);
// Collect all form elements to make it easier to print the whole form.
$vars['search_form'] = implode($vars['search']);
}
Note: you must rename the preprocess function so it's right for your theme (replace mytheme with the name of your theme)
In order for this to work, you must also ensure you have a copy of search-theme-form.tpl.php in your theme directory (you can grab it out of modules/search/ from your Drupal core files). You can then edit this as you like for further theming.
Extending it further.
The same techniques can also be applied for the search block. Just use the code from above, but replace all instances of search_theme_form with search_block_form, and make sure that you take a copy of search-block-form.tpl.php into your theme directory.
Similar articles on this site
LATEST ARTICLES
TODAY'S MOST POPULAR
-
4th Sep 08
-
8th Aug 08
-
20th Sep 08
MOST POPULAR ARTICLES
RECENT COMMENTS
-
1 day 6 hours ago
-
3 days 19 hours ago
-
4 days 30 min ago
-
4 days 6 hours ago
-
4 days 23 hours ago

Comments
16th Sep 2008, 9:48pm
Hey man. Thanks for this. Spent a couple days trying to figure how to get this working.
10th Oct 2008, 6:59pm
Thanks a ton for this. It worked like a charm and also helped me to really understand "preprocess" functions. Is there a way to do the same with the "user login" block? I can try on my own - but I'm not sure where to find the original .tpl files.
1st Dec 2008, 10:38pm
when I do this my submit button jumps under my input box. Any idea how to change that ?
27th Feb 2009, 7:22pm
This is the most conceptually correct way to do it. Thumbs up! Looks great in my Firefox!
2nd Mar 2009, 12:51am
Thank you =) It was very helpful as I'm new to Drupal.
11th Mar 2009, 5:47pm
can this be done on a multilingual site?
18th Mar 2009, 4:51am
Just wanted to point out that if your working in Drupal 6 and have the following code:
function YOURTHEMENAME_preprocess_search_BLOCK_form(&$vars, $hook) {// Set a default value for the search box
$vars['form']['search_theme_form']['#value'] = t('Search');
This will allow you to change the value of the search box. The code above didn't work. Just a quick change and it worked. Hope this helps someone.
26th Mar 2009, 9:14pm
HUGE help, thanks so much for posting this.
31st Mar 2009, 7:15pm
Thanks! This worked as soon as I flushed my Theme Registry. I added a line to customize the class for the submit button.
// Add a custom class to the submit button
$vars['form']['submit']['#attributes'] = array('class' => t('button'));
I was wondering how I could add the javascript for making the default value disappear when you click on the box.
1st Apr 2009, 1:40pm
The very best method for that is a very little but powerful module : Custom Search Box. 5 seconds to configure it, without a line of code for the same result... Have fun !
1st Apr 2009, 2:46pm
Nice module. That will come in handy for future sites. In a future release perhaps you could consider adding the ability to insert the javascript needed to create the disappearing default text when you click on the search box.
24th May 2009, 12:13am
I could not make it work. I am using drupal 6.11 and I probably missed something...
- I copy-pasted the code into template.php
- I changed the name to tendu_default_preprocess_search_theme_form - my theme is tendu_default under tendu parent theme.
- I copied search-theme-form.tpl.php to the sub-theme dir.
After this did not work, I also tried:
- to changed the name to tendu_preprocess_search_theme_form - the PARENT theme
- I copied search-theme-form.tpl.php to the parent theme dir.
Nothing...
Help? Thanks!
24th May 2009, 7:44am
Amir: Did you clear your theme registry (cache)? You need to do this in order for Drupal to pick up the new template. There are 3 ways to do this:
24th May 2009, 11:00am
Thanks for the answer Tom.
Yes, I have cleared the cache (devel module). No help...
25th Jun 2009, 7:54am
I had to remove the second argument $hook to get this working.
This is identical to the number of arguments found in the
modules/search/search.module file.
Other than that its made customization a breeze.
Hope this helps anyone with a similar problem.
25th Jun 2009, 7:44pm
very helpful... thanks. I was trying to read through that same post and with so many replies and opinions on 'the right way' it was hard to make sense of it. Now that I've learned a bit more, though, even this function that re-renders this search form - I've come to realize that even this solution isn't ideal, because it's causing a second render of the form. It would be best to affect the variables before it's rendered the first time and save cpu overhead, not that this is a bad solution: It's certainly better than waiting until the very last second before it hits the browser, basically, and doing a string replacement with an empty string..that just seems so hacky, to me. Of course, I have no idea how to do it(I'm assuming it is with hook_form_alter(),) but I'm learning. If I find a solution online to alter this before it's rendered the first time I will come back and post a follow-up link.
Thanks for this. It's been a key step to really learning what's going on with the way things are rendered for me.
-=- christopher
24th Jul 2009, 7:57pm
For anybody that stumbled upon this, please also see:
http://drupal.org/node/224183
24th Jul 2009, 7:05pm
Sorry for the lack of information there. That page is a great starting spot for anybody looking to modify their search form in Drupal 6 specifically. The comments in the thread eventually point to another excellent method here:
http://ahsangill.wordpress.com/2009/04/29/drupal-6-custom-search-box/
Also, for those with simple needs that just want to modify their search form without too much hassle, there's a module for that:
http://drupal.org/project/custom_search_box
Cheers!
22nd Dec 2009, 1:00pm
HI NICE STUFF....It's really very informative
Please share your thoughts, comments and suggestions...