scriptygoddess

06 Feb, 2009

get_search_form() in wordpress

Posted by: Jennifer In: WordPress|WordPress Hacks|WordPress Plugins

So I've discovered a new feature in WordPress 2.7 – get_search_form(). What this will do is first (for backward compatibility sake) look for a file named searchform.php in the theme directory – and if it doesn't find it then it generates the HTML for you. (=cringe=) Yeah. You just know I've gotta be overriding that.

Thankfully the function does have a filter hook. First – to understand what the function is doing – here is a link to the phpxref of the function.

So you can override this one of two ways. You can simply create a searchform.php page in your theme directory with the search form you want to use… or you can add something like the following to your functions.php file in your theme directory (or create a plugin with this, although that seems like overkill – probably a function in functions.php is fine)

function my_search_form($form) {
$form = '<form method="get" id="searchform" action="' . get_option('home') . '/" >
<div><label class="hidden" for="s">' . __('Search for:') . '</label>
<input type="text" value="' . attribute_escape(apply_filters('the_search_query', get_search_query())) . '" name="s" id="s" />
<input type="submit" id="searchsubmit" value="'.attribute_escape(__('Search')).'" />
</div>
</form>';
return $form;
}
add_filter('get_search_form', 'my_search_form');

The above value of "$form" is the same HTML that WordPress' get_search_form() will spit out. And it has some nice things in it (like pre-populating the search field with the last query requested). So you can start modifying it from there as you need to (ie. removing or changing "Search for" etc.

From a performance standpoint – I'm not sure which would be better – creating the searchform.php or making use of the get_search_form filter. (or is it negligible?)

8 Responses to "get_search_form() in wordpress"

1 | Rajbir D

February 7th, 2009 at 12:42 pm

Avatar

was looking for something like this, but i think it's better to use searchform.php

2 | Jennifer

February 7th, 2009 at 2:06 pm

Avatar

Yeah, I'm inclined to believe that too. Since the first thing the function does it look for the file – once found – that would be the end of any processing. :) Well if nothing else, it was an interesting experiment. LOL

3 | Buzzlair Voufincci

February 27th, 2009 at 11:32 am

Avatar

Okay, the problem with the provided searchform is that

the label tag is annoying and i cant a way to modify it without going to the core engine /include.

but when u applied kubrick default theme. it removes label tag. the question is how do they remove it. i checked its css file but cant find anywhere that could perform such action.

so, i created searchform.php using exactly the same structure of the provided search form but in xhtml format and without the annoying label tag 😀

at least, thats the solution 😀

4 | Rawr

March 18th, 2009 at 7:18 am

Avatar

Having a hard time with this. I've started out with 2.7 so I don't know what the searchform.php is supposed to look like. I found a code on Google Code but that keeps giving me some sort of error. Will try again. If anyone know a link that has full instructions, please share, thanks.

5 | Jennifer

March 18th, 2009 at 11:57 am

Avatar

So if you're going to use searchform.php – just create a file, name it "searchform.php" – put it in the same folder as the rest of your theme files. Add something like the following:

<form method="get" id="searchform" action="<?php echo get_option('home'); ?>/" >
<label for="s">Search:</label>
<input type="text" value="<?php echo attribute_escape(apply_filters('the_search_query', get_search_query())) ?>" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</form>

Then call it from any of your template files using this: <?php get_search_form(); ?> – and it will pull in the contents of your searchform.php file (just like get_header() pulls in the contents of header.php… etc)

6 | Goedkope

June 17th, 2009 at 4:06 pm

Avatar

I have manually moved my search widget into the my header. It works great in Firefox 3, Opera, Chrome. But in IE6 the search form is not functioning.

I have no ideas. Help will be appreciated.

Goedkope

7 | Suzanne

July 11th, 2009 at 6:56 am

Avatar

Merci beaucoup Jennifer

Thank you very much!! I am new with wordpress and I was trying to find a way to have the search button in french or in english (with WPML plugin and default theme)

I took you code to create the seachform.php file and add this code:

<input type="submit" id="searchsubmit" value="" />

and it works fine 😉
Suzanne

8 | Steven McCurrach

September 3rd, 2009 at 5:31 am

Avatar

Thanks alot for the help in regards to creating the searchform.php file!
I'm bookmarking your site.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements