Create a Custom Search Box with Thesis

I’ve been working on realigning my blog a bit the past two days. It is running on Thesis, which I am treating more as a WordPress framework than a theme, so I’ve been doing quite a bit of work to get rid of the default look and feel that the theme comes with.

A huge bonus of using Thesis is that most of the typography decisions are made with the theme’s control panel, so that gives me a chance to work on little details, like the search box. So, how’d I make a fancy search box?

First, lets get one thing straight: This is not a custom search box in terms of markup. This is the exact same, default XHTML that Chris includes with Thesis, all I did was position it where I wanted in the theme and style it to look the way I wanted.

Step 1: Add the Search Box to Your Theme

Using Thesis’ hooks you can position the search box anywhere: within the navigation, inside a sidebar, even inside every single post (I wouldn’t recommend that). I positioned it to the right of my tabs.

// Remove default navigation
remove_action('thesis_hook_before_header', 'thesis_nav_menu');
 
//Create my new markup
function bleikamp_nav() {
	echo '<div id="nav">';
	echo '<div class="nav_container">';
	thesis_nav_menu(); // Default navigation tabs
	thesis_search_form(); // The default search function
	echo '</div>'; 
	echo '</div>'; 
}
 
// Hook in my new navigation + search
add_action('thesis_hook_before_header', 'bleikamp_nav');

What am I doing here?

  1. I disable the default Thesis navigation because I want to wrap it in my own containers
  2. I create my new navigation function. I actually reuse the Thesis function I disabled (thesis_nav_menu) and drop in the default search box (thesis_search_form).
  3. I hook my new navigation + search function into the theme before the header.

Step 2: Style It

#s {
     float: right;
     background: url("images/bkg_search.png") 0 0 no-repeat;
     color: #fff;
     padding: 6px 10px 6px 30px;
     width: 156px;
}

This is where all the “magic” happens. Until you add this CSS, you have a boring looking search box sitting right next to your tabs.

I floated the search box to the right hand side of the navigation container. The tabs are floated to the left side.

bkg_search

I add a background image to the search box, which is how I achieve the rounded corners and get the icon into it.

I set the text color to white, since I am using a dark background.

The padding is important. I am using shorthand CSS, so the numbers mean top, right, bottom, left. The search box is still square as far s the browser is concerned, so the padding helps us position the text correctly on top of our background image.

  • The top and bottom padding are what center the text in the box and display the entire height of the image.
  • The left and right padding keep the text from running over the icon or running too far into the curves and looking “off.”

Will this work on all browsers?

I have no idea, I didn’t test it. But in general my code is completely functional in every browser, but may not look perfect. There are more “bulletproof” ways to do this, but this is my personal site and I am basically designing this for people who use modern browsers.

Also, this CSS is not unique to Thesis and can be used to style any text input you want.

{ 4 trackbacks }

This Week in Thesis โ€” January 4, 2009
May 5, 2009 at 11:27 pm
Thesis101 » Blog Archive » Move Search Box to the Right of Navigation
June 8, 2009 at 8:38 pm
[GET]Great Resources for WordPress Thesis Theme Users - 2790th Edition | marketFly
June 9, 2009 at 2:21 am
27 Links to help you with Thesis โ€” Serradinho
June 18, 2009 at 12:43 pm

{ 10 comments }

1 Kyle January 4, 2009 at 7:41 pm

A really cool way to customize your search box (in Safari, at least) is to use type=”search”–it gets nicely styled, and it also includes search history.

Here’s a nice post I found on it: http://alexking.org/blog/2006/11/12/safari-search-boxes

2 grenuis January 4, 2009 at 9:38 pm

HI, I copy and paste the codes but it seems that it’s not working for me, it just appears bunch of codes in my main pages, can anyone guide me through this? Thanks!

3 Ben January 4, 2009 at 9:54 pm

grenuis: You need to paste the PHP into your custom_functions.php file and the CSS goes in your custom.css file (I am assuming you’re using Thesis).

Also, it is important to note that these code bits are intended as examples, the containers, etc. are not set up to work on your page. You’ll have to figure out how to create all that yourself.

4 Ben January 4, 2009 at 9:56 pm

Kyle : I personally don’t use Safari so I get annoyed when people’s sites are designed with Safari specific code in mind.

5 Kyle January 4, 2009 at 11:19 pm

Ben: To be honest, I don’t use Safari either–but it’s a neat trick to make the searching experience for Safari users better, and it degrades gracefully.

6 kang at Londoneater dot com January 14, 2009 at 10:16 am

The more I read these cool customisation tricks, the more I wish I knew how to transport all my v1.1 custom stuff to v1.3 without too much time lost figuring out where to put what code.

sigh, I’m loving your blog design by the way - thesis user too, but ain’t a pro like you are! Great tutorial, I’ll be using this when v1.4 comes out.

7 kalani March 26, 2009 at 4:11 pm

hey Ben, any idea on how I can get your beautiful search field to float above my custom nav menu next to the rss icon ? Thanks in advance. 3 days and haven’t found any solutions nor have the several php forums I’ve been looking for answers in.

8 Alyssa Suffredini April 7, 2009 at 8:11 am

This all worked perfectly for me, except that the search box is below the navigation and not in line with it. Can you help me?

The site is a disaster right now, so please disregard the ugliness…

Thanks!

9 Mike @ TheThriftyLife April 24, 2009 at 8:47 pm

@alyssa - Try reversing these lines. You may need to have the search box float right before the nav items float left.

Find this:
thesis_nav_menu(); // Default navigation tabs
thesis_search_form(); // The default search function

Change it to this:
thesis_search_form(); // The default search function
thesis_nav_menu(); // Default navigation tabs

10 Matt April 27, 2009 at 2:41 pm

I’m using Thesis 1.5b7 on WP 2.7.1.

I have a custom header image.

How can I get the search box and RSS icon into my header?

Thank you,

Matt

Comments on this entry are closed.

Previous post: Fox News is an Example in Bad Design Decisions

Next post: I am not a Malcolm Gladwell Fan