Welcome to Dagon Design. In addition to free scripts, WordPress plugins, and articles, we offer a variety of services including custom theme design, plugin creation, and PHP scripting. Contact me for more information.

Updated Saturday, February 21st, 2009 at 1:13pm

WordPress Search Form without Search Button

This article explains a method for displaying a WordPress search form without a search button. The user simply enters they text they would like to search for, and hits enter. While this is easy to do with the use of a hidden submit button, this method also allows for the use of a default message in the search box (such as “Enter your search”), which keeps you from having to display a label next to the form explaining what it is for. This default message automatically clears when the user clicks to start typing, and puts itself back if the user clicks away. This allows you to have a search form that looks nice and clean, and is still just as functional.

The code

(extra newlines have been added to the code to make it fit the page properly)

<?php $search_text = "Search"; ?> 
<form method="get" id="searchform"  
action="<?php bloginfo('home'); ?>/"> 
<input type="text" value="<?php echo $search_text; ?>"  
name="s" id="s"  
onblur="if (this.value == '')  
{this.value = '<?php echo $search_text; ?>';}"  
onfocus="if (this.value == '<?php echo $search_text; ?>')  
{this.value = '';}" /> 
<input type="hidden" id="searchsubmit" /> 
</form>

Usage

Most WordPress themes have a file called searchform.php which contains the code that generates the search form. All you generally have to do is replace the existing code with the code above.

For themes without the searchform.php file, you can place this code directly into your theme files, wherever you would like the search to go.

Changing the default text

To change the default text of the form, you just have to configure the $search_text variable in the first line of the code.

Styling the form

Using CSS, it is easy to adjust your form. For example, if you want to define the exact width of the search field, you could wrap a div around it like this:

<div class="search-form">
(form code goes here)
</div>

And then in your CSS file, add the following:

.search-form input {
  width: 150px;
}

Example

You can see an example of this method in the left sidebar of this website.

  If you have found this page useful, please consider donating. Thanks!

  1. 40
    devbrat

    please send me code of search

  2. Perfect, thank you!

  3. Thank YA infonya……byponsel.com

  4. WA…. thank To your Information ………BYPONSEL.COM………..

  5. Thiago, of course it will work with it. It doesn’t change anything, just the look…

  6. Do you know if any plugin will function correctly with this form? I mean Search meter (wordpress).

  7. great script

  8. can we put google adwords there..??

  9. Hi – got this working the side-bar – very nice.

    But is it possiple also to have the search box appear directly in a page or post? Either only on the page or on the page and in the sidebar. And can it be inserted directly in the editor?

    Thanks, Michael

  10. thanks a lot.. works fine

  11. Great post, your code does exactly what it says on the tin. It has been surprising how hard this info has been to find on the matrix aka google. So hats off, great job.

    I used searchform.php and some css for implementing code into my blog.

    Let me know you think.
    http://solreka.com/blog

    Once again, many thanks for the great post.

  12. No searchform.php file in your theme?

    I didn’t have one, either. So I made one. This was easy. I could not figure out how to paste the code in my sidebar.php file. WP looks for a searchform.php file before defaulting to whatever it defaults to.

    In my webhosting account’s control panel, I opened the file manager, then went to this directory:
    public_html/your_wp_directory_name/wp-content/themes/your_theme_name
    Create a new file here
    Open file
    paste in above code
    Save

    This can now be modified from WP dashboard (Appearance > Editor > your_theme_name > searchform.php

    Thanks for the snippet!

  13. This method has been confirmed to work with the latest release of WordPress – 2.8

  14. I was having trouble and getting extremely frustrated making the search button consistent across all browsers. I stumbled upon this entry and found it very handy. Thanks a bunch.

  15. 26
    MadMax

    Hello!

    Thank you for sharing this great Code!
    Very lean solution!
    One thing that might help other users:
    My theme had no searchform.php, so I just saved your code as searchform.php, put it into my theme folder and it works greatly!!!
    I´m not sure if this works for older WP-versions, though.
    I think I read somewhere that it´s a new feature of WP 2.7.1 that automatically searches for a file named searchform.php, but, as I said, I´m not sure if that´s true.

    Thanks to you again, greetings, MadMax

  16. Hi.. great bit… thanks for sharing! I wanted to second John’s request for a way to have this validate.

  17. This code modification has been tested in the latest release of WordPress (2.7.1)

  18. This works fine, and seems like a great solution.
    Only when I try to validate my site, W3C throws up a couple of problems which I can’t seem to fix. Is it possible to reformat the code so that it validates?

  19. Hi i have a question, would appreciate a lot if someone could help me in this….

    I’m using WP as CMS and works fine everything BUT, when calling the search function having a little trouble, that is: My site is a multi language site, when searching a item, it still bring me to the default language. i have dutch and english, the default language is dutch, so when someone switch to english and want to search for something the result page shows in dutch, i need to add a something like : if( isset($_GET['lang']) ){echo ‘&lang=’.$_GET[lang]; }

  20. Thanks for this great peice of code! combined with advanced search plugin…this solution rocks my clients site.

  21. Hi; and thanks for the script. It’s works but my theme is very old?
    I see this when I make a search:

    Fatal error: Call to undefined function get_search_form() in /home/extran5/public_html/legales/wp-content/themes/default/sidebar.php on line 12

    Do you have any idea to fix this?

  22. 19
    matteo

    Hi, I just added the code to my blog but the search field it just comes out way too big. I tried to change the height value in the CSS under the width value but it didn`t change anything.

    Now the code it look like this:
    .search-form input {
    width: 150px;
    height: 30px;
    }

    What else can I do?

  23. is there a way to add different blogs in the php script via a drop down menu or something?

    i would like to link three or more WP blogs on same domain (different subdomains) in one search form in wordpress

    if anyone knows a way to do this please drop me a line

    thanks

  24. 17
    yogesh

    please tell me
    what is the coding of searching the particular product among 100 products in php?? i.e of search button

  25. thanks for this great code ; and for the way you help loosers like me to implement it on their blog without any pain.

    thanks !

  26. Cheers for the code snippet. Was number 1 on Google and worked a treat.

  27. Thanks for this!

  28. I have placed the above code in my searchform.php file and uploaded it to my theme. I disabled the widget in the admin section of wordpress and have the include script for the searchform.php on my sidebar.php file.

    The problem is that I do not see any search box in my side bar now. it will only show the widget. When the widget is off there is no search. Any one know whats going on?

  29. Thank you. It’s rare when you do simple search, find a solution, and it works perfectly. I am using a nice template (Chaotic Soul) but either my search button or the default text was not showing. A simple cut-and-paste and a quick edit of the default text and I have exactly what I wanted. Thanks again for you work.

  30. Jakob: The simple answer is that that function makes input from a visitor safe to display on the screen. Since some search forms show what the user typed in, when the search results are shown. The method above does not do this :)

  31. 10
    Jakob

    Oh! Sorry for the danish words in my form :-)

  32. 9
    Jakob

    Here is what I use:

    
    <form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
    <input type="text" onclick="if(this.value=='&nbsp;Indtast s&oslash;geord her') { this.value=''; }" value="&nbsp;Indtast s&oslash;geord her" name="s" id="s" />
    <input type="hidden" id="searchsubmit" value="Search" />
    </form>
    

    In many WP search forms you’ll see that the value of the input field is set to:

    value="<?php echo wp_specialchars($s, 1); ?>"

    Why? What does it do? And is it important?

  33. Lisagh: The code above just changes the way the search button is displayed. It does not change the way the results are generated.

    It is probably showing the protected posts because you are logged in. If you log out and search they should not appear.

  34. 7
    Lisagh

    Love your search box. However, when I put it in on my wordpress blog, it all posts, even my PROTECTED POSTS. Do you know how to limit it to showing only unprotected posts after a search?

  35. matthijs: Thanks :)

  36. 5
    matthijs

    A few alternatives are
    1) use #
    2) use htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES,
    ‘UTF-8′); or
    3) basename(__FILE__)
    Some good info here http://blog.phpdoc.info/archives/13-XSS-Woes.html

  37. matthijs: I just changed it to use bloginfo to get the path :) Thanks for pointing that out. What method do you generally prefer (outside of WP), out of curiosity?

  38. 3
    matthijs

    The use of PHP_SELF is not so wise, as it can contain tainted data (from the client). So do not use that variable or make sure you escape it.

  39. Christian: Perhaps. The WordPress support forums would be a better place to ask though. The comments here are primarily for the discussion of the article.

  40. Is there a way to include a SEACH FIELD (of the Wordpress Database) on a EXTERNAL WEBSITE?

Leave a Comment

Before you comment: If you are having an issue with a script, please make sure you have read the entire article. Also, please read through the comments because most common issues have already been discussed many times. Thanks.


Be sure to wrap all code in <code></code> tags.