Add the WordPress search function to your Thesis header

November 16, 2009 · 12 comments

UPDATE: 18 November 2011 – Added JavaScript to clear the input field when clicked on.

Add the WordPress search function to the Thesis header

Today’s Thesis recipe is a quick and simple fix that adds the WordPress search function to the Thesis header. As with any Thesis customization, you’ll need to have the custom_functions.php and custom.css files open.

Step 1 – Add the Search function

Open your custom_functions.php file and copy the following code into it:

function custom_search_widget() { ?>
<div class="widget widget_search">
	<form method="get" class="search_form" action="<?php bloginfo('home'); ?>/">
		<p>
			<input class="text_input" type="text" value="<?php echo $field_value; ?>" name="s" id="s" onfocus="if (this.value == '<?php echo $field_value; ?>') {this.value = '';}" onblur="if (this.value == '') {this.value = '<?php echo $field_value; ?>';}" />
			<input type="submit" id="searchsubmit" value="Search" />
		</p>
	</form>
</div>
<?php } 

add_action('thesis_hook_header' , 'custom_search_widget');

To break this down, you’re creating a custom function (custom_search_widget), you’re adding the WordPress search form and you’re telling Thesis (by means of a hook) to add the custom function to the Thesis header.

EDIT: You could also use the ‘thesis_search_form’ function that’s built in to Thesis, which would mean you can skip this function code altogether and simply add the action. I forgot about that one. Only catch is, it doesn’t have a submit button.

Step 2 – Style the Search function

Your search function will work just fine, but it looks lousy. If you’d like to clean it up and e.g. position it on the right hand side of your header, add the following to your custom.css file:

/* clears the header floats */
#header {
	overflow: hidden;
}

/* allows the search function to be positioned to the right of the logo */
#logo, #tagline {
	float: left;
	width: 300px;
}

/* places the tagline below the logo */
#tagline {
	clear: left;
}

/* positions the search widget on the right hand side of the header */
.widget_search {
	float: right;
	width: 200px;
}

/* formats the search input and button */
.search_form .text_input {
	font-size: 11px;
	padding: 5px;
}

.search_form #searchsubmit {
	font-size: 11px;
	padding: 4px;
}

There you have it. Obviously you can style your search form to match your theme, but the bare-bones are here. All that’s needed now is your imagination (and some CSS skills).

If you think these short tutorials are useful or you’d like to request a Thesis recipe, please let me know.

Subscribe for free WordPress tips & tricks

Every day I learn something new and interesting about WordPress. Subscribe to my spam-free newsletter and I'll send you a monthly digest with the best stuff I've found online.

Share this:

{ 12 comments… read them below or add one }

Kim Woodbridge November 17, 2009 at 2:44 pm

This is great Dave. We really need these straightforward tutorials for Thesis.
.-= Kim Woodbridge´s last blog ..Upcoming Facebook Changes to Boxes and the Box Tab that Will Impact Your Custom Fan Pages =-.

Dave November 17, 2009 at 2:50 pm

Hi Kim, I think that it’s these simple customization elements that keep people from exploring Thesis further. Hopefully 100 or so of these tutorials will help the Thesis community to muster up some unique designs :)

barbara December 1, 2009 at 12:15 pm

An excellent tutorial, and it actually works! I’ve had an unhealthy obsession about getting a search submit button into my Thesis themed blog — http://moderncomfortfood.com — since, oh, the day I installed Thesis. And each attempt failed until I saw your tutorial above.

I have a couple of follow-up questions though:
1) I want the Thesis search form to stay in its default widget position in the sidebar. But substituting “thesis_hook_before_ sidebar_1″ bumps the search form to the top of the sidebar, where I don’t want it. Any ideas about how I can get the custom.php to apply to the default widget without actually moving it?
2) I’ve tried various iterations of substituting a 24px x 20px .gif search image for your search submit button — a la background: url (http://…. It’s not quite working. Any ideas?
.-= barbara´s last blog ..Turkey or Chicken Divan =-.

NIraj August 28, 2010 at 2:45 pm

Dave,
this trick works perfectly but only one thing is, Search button is no click-able but it you try hitting enter it works.
so how to make Search button click-able?

Regards,
Niraj

Ted Hessing September 4, 2010 at 10:45 pm

Great tutorial, Dave!

I think you may have changed your content column width after writing this piece – the header image is still at 800px and is over flowing!

Thanks for the help!

Microsoft Office Online Training September 17, 2010 at 10:43 am

thanks. I kinda got this workign in the right spot but need to tinker with it a bit until I’m happy.

Mark Hockenberry November 20, 2010 at 4:08 pm

Thanks Dave,

I really like the results of this code except for one issue, The text ” Enter Search…” doesn’t go blank (disappear) when you click in the box to search. Here is my site I want to use this on: http://rightonwebdesign.com
any suggestions?

Paz February 17, 2011 at 8:07 am

I used this with great success, however I want the form to sit about 10 pixels from the top of the header. Is there a function that I can add to do that as well.

Dave February 22, 2011 at 3:31 pm

@Paz, you’d need to use some custom CSS (in your custom.css file) to achieve this. Depending on your layout, applying the following may help:

.search_form {margin-top: 10px;)

Sarah January 6, 2012 at 5:24 pm

Thanks for the great tutorial Dave.

I’m trying to move the search bar to the top right corner without losing the clickability and formatting of the two images I have in my header. I’ve tried tinkering around with the CSS to no avail.

Any suggestions?

Laura January 13, 2012 at 6:00 pm

Hi, would you by any chance know where the search box text ‘to search, type and hit enter’ is hidden, in THESIS, so I could translate it for a project ?
Thanks much !
Laura

Alison Jardine January 16, 2012 at 11:16 pm

Hello Dave

Wanted to express my appreciation for this code! I actually managed this all by myself on my site, that’s how good your explanation is :) )
My little search box now appears neatly on the top right of my site, occluding my banner.
Appreciate you posting this very much….
Alison

Leave a Comment

Previous post:

Next post: