A search box in main menu is something a lot of themes have by design. Although this is not the case with the Thematic Theme Framework, it’s easy to customize it with a few lines of code.

First thing we need to do is create a thematic child theme. This way we won’t make direct changes to Thematic.

The search box will be displayed on the right of the menu, inside of the #access div. The way Thematic hooks and actions were constructed will first need to remove the thematic_access() action from the thematic_header() hook. If all this sounds a bit difficult fear not. All you have to do is add this code inside of functions.php file of your child theme (don’t forget the <?php opening tag and the ?> ending tag).

// Remove default Thematic actions
function remove_thematic_actions() {
    remove_action('thematic_header','thematic_access',9);
}
add_action('init','remove_thematic_actions');
</php>
 
If everything is working properly when you reload your Wordpress installation the entire menu would have disappear! <strong>Now we can create our own menu WITH the search box on the right of it! </strong>Add this code at the end of your <em>functions.php</em>:
 
<pre lang="php">
// Create a custom access div with the menu and search box
function search_access() { ?>
    	<div id="access">
    		<div class="skip-link"><a href="#content" title="<?php _e('Skip navigation to the content', 'thematic'); ?>"><?php _e('Skip to content', 'thematic'); ?></a></div>
            <?php wp_page_menu('sort_column=menu_order') ?>
 
			<div id="access-search">
				<form id="searchform" method="get" action="<?php bloginfo('home') ?>">
					<div>
						<input id="s" name="s" type="text" value="<?php echo wp_specialchars(stripslashes($_GET['s']), true) ?>" size="20" tabindex="1" />
						<input id="searchsubmit" name="searchsubmit" type="submit" value="<?php _e('Search', 'thematic') ?>" tabindex="2" />
					</div>
				</form>
			</div>
 
        </div><!-- #access -->
<?php }
add_action('thematic_header','search_access',9);

Reload your page and the search box will be there, but not where you expected it to be! We’ll need to add a bit of css styling to it.

Go ahead and open the style.css and at the end of it add this css code:

#access-search{
	position:absolute;
	left:50%;
	width:260px;
	margin-left:170px;
	height:29px;
	border-top:1px solid #ccc;
	border-left:1px solid #ccc;
	border-right:1px solid #ccc;
	padding:2px 0 0 4px;
}

This is it! You now have successfully added a search box to the right of your thematic main menu!

Preview of the finished search box
By This entry was posted in Recomended Reading, Thematic, Theme Design, Wordpress and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

47 Comments

  1. Posted April 15, 2009 at 3:20 am | Permalink

    Great work Cristi!

  2. Posted April 15, 2009 at 8:42 am | Permalink

    Thank’s Danny! ;)

  3. Posted May 6, 2009 at 2:06 pm | Permalink

    Great little tutorial. Thanks so much!

    b

  4. Posted May 7, 2009 at 6:54 am | Permalink

    Thank you very much!

  5. Malcolm
    Posted June 12, 2009 at 1:41 am | Permalink

    Hey…first part works great!
    Not too much luck with the second part…

    I get the following error
    Parse error: syntax error, unexpected ‘?’ from the first line of the second function?
    function search_access() { ?>

    Thanks for your help!

    • Posted June 12, 2009 at 8:58 am | Permalink

      Something went wrong with the syntax highlighter and all sort of characters appeared in the code. I’ve updated the code on this page. Please try again and tell me if it worked for you.

      • Malcolm
        Posted June 12, 2009 at 4:25 pm | Permalink

        Works perfectly!

        Thanks for all of the help…

  6. Karen
    Posted July 16, 2009 at 12:37 am | Permalink

    The code works great and displays correctly in IE8, Firefox 3.5, and Opera 9.64 in Windows Vista.

    However, in IE6 in Windows XP, IE7 in Windows XP, and Safari 4.02, the menu displays about 50 pixels below where IE8/Firefox 3.5/Opera 9.64 display.

    I know I can target IE6 and IE7 with conditional comments, but don’t know any way to target Safari.

    Do you have suggestions for how to resolve the drop?

    • Posted July 16, 2009 at 7:25 am | Permalink

      I know about the bug in IE, just that I never had a chance to fix it.

      To target different browsers is actually easy to do. Thematic has a body class. If you open the source code of your site (where you’re using thematic) and you have a look at the tag you’ll see it has a lot of classes. The last class it’s the browser class. So if you’re on firefox the body will also have the class: firefox ff3, and if you’re on IE7 the body will have the class ie7.

      Now you just have to target the css you need like:

      .ie7 #access-search {
      /*code for IE7 only*/
      }

      This way you won’t have to use hacks.

  7. Karen
    Posted July 16, 2009 at 1:38 pm | Permalink

    Thanks Cristi.

    What I discovered was missing in the CSS styles above was the “position: relative” on the container of the search box. Without “position: relative”, the “position: absolute” on the “access-search” will not position correctly.

    Once I added “position: relative” to the “access” div, the “access-search” box positioned correctly.

    I did use conditional comments to target IE initially, but discovered I didn’t need the conditional comments once I added “position: relative.”

    A question for you: how would you add the search box to the .menu div, as Ian did in his “The Break” theme used on the http://www.themeshaper.com site?

  8. Karen
    Posted July 16, 2009 at 1:40 pm | Permalink

    Wow! I didn’t know Thematic had the functionality built in to target specific browsers. Makes this theme better and better. Thanks!

  9. Posted August 14, 2009 at 3:08 pm | Permalink

    @Cristi,

    Thanks a lot for posting this function. It’s exactly what I needed in my Thematic child theme!

  10. Mike Miller
    Posted August 20, 2009 at 3:27 am | Permalink

    I understand all of this and it works great, but my question (maybe a little off-topic) is while I know what the third attribute (the number 9 in this instance) indicates the position, how do we determine what number should be used there?

    Mike

  11. Posted August 28, 2009 at 1:09 am | Permalink

    Is it correct/appropriate to use the following code in place of your provided code to insert the search box? I’m calling thematic_search_form() instead of building the form from scratch:


    // Create a custom access div with the menu and search box
    function search_access() { ?>

    <a href="#content" title="">


    <?php }
    add_action('thematic_header','search_access',9);

    The functionality seems to work just fine. Is one method preferable over the other?

    I’m having a little trouble styling the new search box. I have added the following to hide the search button:


    #access-search #searchsubmit { /* Hide "Search" button */
    display: none;

    That’s about as far as I have gotten with the styling…

    • Posted September 1, 2009 at 3:14 pm | Permalink

      Oops. Looks like the code didn’t come through properly on my comment above. Basically, I’m replacing:
      <input id="s" name="s" type="text" value="" size="20" tabindex="1" />
      <input id="searchsubmit" name="searchsubmit" type="submit" value="" tabindex="2" />

      with:

      • Posted September 1, 2009 at 3:16 pm | Permalink

        Hmm…frustrating. Basically instead of building the form I’m calling the thematic_search_form() function. But, I can’t figure out how/where to style it.

        Sorry for the multiple comments…

        • Posted September 1, 2009 at 3:18 pm | Permalink

          Look click view source in your browser and see that html that function generates. After that write css for it.

  12. boerenooi
    Posted October 23, 2009 at 10:19 am | Permalink

    Hey Cristi

    How do I add placeholder text in the searchbox?

    Thanks :)

  13. Posted November 26, 2009 at 2:36 pm | Permalink

    Hi Guys,

    Im getting two problems? Any Help?

    FRONTEND:
    Warning: call_user_func_array() [function.call-user-func-array]: First argumented is expected to be a valid callback, ‘search_access’ was given in /homepages/31/d239607629/htdocs/wordpress/wp-includes/plugin.php on line 339

    BACKEND:
    Warning: Cannot modify header information – headers already sent by (output started at /homepages/31/d239607629/htdocs/wordpress/wp-content/themes/gallery/functions.php:10) in /homepages/31/d239607629/htdocs/wordpress/wp-includes/pluggable.php on line 850

  14. Posted November 26, 2009 at 3:28 pm | Permalink

    So,,, I got this baby working! However, how do we display the results like gallery too?

    http://www.1001vectors.com is address, helllp!

    Joe

    • Posted November 26, 2009 at 3:32 pm | Permalink

      Looks Good.

      I’m not sure, but probably you should replace the search loop with the index loop. I’m presuming this was done using hooks and filters inside the “Gallery Theme” functions.php. I never worked with this child theme so I can’t really help you!

  15. Posted November 26, 2009 at 3:57 pm | Permalink

    Can you help me! If you can see all the code?

    Joe

  16. Posted January 23, 2010 at 2:12 pm | Permalink

    Really cool use of Thematic hooks and filters! Just applied it to a Thematic child theme I’m developing. Thanks Cristian!

    • Posted January 23, 2010 at 7:10 pm | Permalink

      Cool! Really happy to hear you’re starting to work with Thematic!

      Maybe you can send me an email with a link to it when it’s done ;)

      Cheers.

  17. Aaron
    Posted January 28, 2010 at 6:26 pm | Permalink

    Could you please advise me. I am getting a seriously bizzare error in IE7 with the search bar, it’s moving down below the aside and into the main div.
    WP 2.9.1
    Thanks,
    Aaron

    • Posted January 28, 2010 at 7:05 pm | Permalink

      Add this to the css:

      .ie7 #access-search {
      margin-top:-32px;
      }

  18. joe
    Posted May 1, 2010 at 8:33 am | Permalink

    i have follewed the above and just can not get my search box to line up with the menu. I started by copy&paste your code & have then tried to amend the css. But just can not get it to move up to be in line with the menu.

    the thematic child theme is at:
    http://www.51degreesnorth.net/framework/

    can u help?
    what am i missing?

    btw many thanks for the tutorial – got me started of where to look to change things

  19. Posted May 1, 2010 at 1:47 pm | Permalink

    Hi Joe,

    I just went to your site to take a look and didn’t see the search box you’re referring to above. Maybe you’ve taken the code out?

    • joe
      Posted May 1, 2010 at 4:02 pm | Permalink

      that’s what i was wondering 4 a bit! – but if you hit the tab key you see it appear, it is there just hidden.

      • Posted May 2, 2010 at 5:18 am | Permalink

        Hey Joe,

        In the css for #access-search, try adding this declaration:
        margin-top:-32px;

        That should get you headed in the right direction…

  20. joe
    Posted May 2, 2010 at 12:02 pm | Permalink

    cool you’re a gent, works a treat
    thanks alot
    joe

  21. Posted May 27, 2010 at 9:48 pm | Permalink

    still works on 2.9.2
    thanks!

  22. Posted June 10, 2010 at 2:43 am | Permalink

    thanks for this. now to read it and understand why it happened.

  23. Posted August 2, 2010 at 9:36 am | Permalink

    Hi, I have used this in my website , hope you can add more info like customizing the search box colors or search button image. Thanks for the help =)

  24. Posted October 17, 2010 at 5:25 pm | Permalink

    This breaks with the latest update to Thematic. Using Thematic 0.9.7.6 or greater you simply could use the following code and omit the remove_thematic_actions()

    function childtheme_override_access() { ?>

    <a href="#content" title="">

    <form id="searchform" method="get" action="">

    <input id="s" name="s" type="text" value="" size="20" tabindex="1" />
    <input id="searchsubmit" name="searchsubmit" type="submit" value="" tabindex="2" />


    <?php }

  25. Posted October 31, 2010 at 3:51 am | Permalink

    Thanks!

    This was a life saver on a project I’m helping with (new design with this trick coming in November).

    Since I’m using 9.7.7, I did it this way to adjust per Gene Robinson’s note above:

    function mysite_new_menu(){ ?>

    <form id="searchform" method="get" action="”>

    <input id="s" name="s" type="text" value="” size=”20″ tabindex=”1″ />
    <input id="searchsubmit" name="searchsubmit" type="submit" value="” tabindex=”2″ />

    <?php }
    add_action('thematic_header','mysite_new_menu',9);

  26. Antoine
    Posted November 16, 2010 at 9:52 pm | Permalink

    Nice tuto, thx !
    But the thematic menu disapeared with this code, after some grabbin on thematic_acccess function code (header-extensions.php), I manage to make it appear again.

    Here’s the code I use (at your own risk, i’m a noob – ie, feedback welcome) :


    // Create a custom access div with the menu and search box

    function search_access() { ?>

    <a href="#content" title="">

    <form id="searchform" method="get" action="">

    <input id="s" name="s" type="text" value="" size="20" tabindex="1" />
    <input id="searchsubmit" name="searchsubmit" type="submit" value="" tabindex="2" />


    <?php }
    add_action('thematic_header','search_access',9);

    • Posted June 2, 2011 at 10:25 pm | Permalink

      Yeah, I had the same problem, I just removed the “remove_thematic_action()” and fudged around to get it to show. I will try your code though, because now I have two access divs. eeeek!

    • Julia
      Posted September 11, 2011 at 7:54 pm | Permalink

      I know this was a year ago BUT can anyone show me the code they used to make the thematic menu reappear? I used the code shown in the tutorial and it made the search form appear in the access area but my menu was gone. I’m not very good with php and I don’t understand how to fix the code Antoine posted. Any help is greatly appreciated.

      Thanks

      • Posted September 12, 2011 at 11:15 am | Permalink

        Hey Julia,

        Search for remove_thematic_action() inside functions.php of your Thematic Child theme. If you find it remove it and see what happened. If not contact me via http://www.cozmoslabs.com/about/ and if you send me your FTP credentials I’ll do it for you.

  27. Antoine
    Posted November 16, 2010 at 9:54 pm | Permalink

    Sorry, I paste the wrong code…


    // Create a custom access div with the menu and search box

    function search_access() { ?>

    <a href="#content" title="">

    <form id="searchform" method="get" action="">

    <input id="s" name="s" type="text" value="" size="20" tabindex="1" />
    <input id="searchsubmit" name="searchsubmit" type="submit" value="" tabindex="2" />


    <?php }
    add_action('thematic_header','search_access',9);

  28. Antoine
    Posted November 16, 2010 at 9:55 pm | Permalink

    sorry again, i don’t manage to post my code…

  29. Posted April 6, 2011 at 12:09 am | Permalink

    – Авторитетный сайт про keywords. На этом интереснейшем ресурсе вы сможете узнать про lava salamander.

  30. Posted June 2, 2011 at 10:07 pm | Permalink

    Man o Man. I love this code. Thanks so much for saving me so much time!

4 Trackbacks

  1. [...] Articolo scritto da Cristi con il titolo Add a search box to the Thematic menu. Tradotto con il consenso dell’ Autore. [...]

  2. By Hej Internet! - Nya bokmärken on September 26, 2009 at 1:02 am

    [...] bokmärken 22 March 2009 | Länkar | Niklas Jakobsen | Inga kommentarer Add a search box to the thematic menu Felipe Skroski » 3 Level navigation menu in wordpress Crazy Egg – visualize your [...]

  3. [...] Add a Search Box to the Thematic Menu [...]

  4. [...] Thematic: Add a Search box to Thematic menu http://www.cozmoslabs.com/2009/04/15/add-a-search-box-to-the-thematic-menu/ [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

* Copy this password

* Type or paste password here:

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>