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. 95
    Scott Levy

    Nice post. I found this blog with withbest practices for creating a search form that is quick and easy to use http://blog.caspio.com/web-database/7-tips-for-creating-user-friendly-search-forms/

  2. Thank you!

  3. Word press is just doing the best.

  4. so great.very nice

  5. research center

  6. Can become the luxury goods

  7. ?the pier at Gulfport precisely where riggings of shrimp boats are loose stitches

  8. Thanks for the article, I’ll think about what you mentioned in it. It’ an interesting point of vie

  9. Only Christian Louboutin can deliver drool-worthy pumps that make our hearts skip a beat!

  10. I always wished to write in my site something like that.

  11. Great Site, your plugins are very useful and save me a tonne of time.
    
    khy

  12. This is a wonderful site! I’ve been looking for something like this 
    for a while now! Thank you!
    gftr

  13. Thank you so very much for taking the time to share…very useful, indeed!fgrr

  14. Thanks for a nice share you have given to us with such an large collection of information xxdffd

  15. Seems pretty easy. Would be great to see some improvement with wordpress’ search though…

  16. Hope the system would work for everyone.

  17. Thanks a lot for making the system more easier.

  18. Thanx for this usefull information! I like your point of view, this matter sorted out your thoughtful blogger exchange is my honor.

  19. Thanks for sharing such a good article, I like your point of view, this matter sorted out your thoughtful blogger exchange is my honor, I wish I could, and you become good friends, for more a more in-depth exchanges!
    cogumelos magicos

  20. 76
    sun

    Very interesting discussion glad that I came across such informative post. Keep up the good work friend. golf trolleys reviews

  21. I visited this page first time and found it Very Good Job of acknowledgment and a marvelous source of info………Thanks Admin!

  22. Very nice and workable method.. Thanks for sharing.

  23. It is indeed a very appreciated idea. Thanks for sharing.

  24. Looks like you are a specialist on this because you just made it so easy to be with you, motivated me to learn more on the subject! May I ask you, do you devote a lot of effort to it because you seem to be so in tune with Medical Assistant

  25. I checked out SWEET and Events.org (who we used) was far better priced then them. Educate

  26. Wordpress search really sucks ball. I wish they could make it better.

  27. I want to be a part of this mags. I have idea to make a good design and to make a good layout for this mags. We need to make other people to feel interesting by see our mags cover, then they will buy itbachelors criminal justice

  28. 68
    sun

    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. health information management degree

  29. please read through the comments because most common issues have already been discussed many times. Thanks. information security degree

  30. Thanks for this I’ve used to to good effect on my training courses web site.

  31. Thanks you very much for this hack, you can see it in action on my website.master in sports mangement

  32. Thanks you very much for this hack, you can see it in action on my website.

    masters of health administration online

  33. I created a nice function for this on my website
    http://www.orangedevdesign.nl/nieuws/wordpress-custom-searchform/

  34. Thanks you very much for this hack, you can see it in action on my website.

  35. Thanks so much! Great plugin.
    mp4 to dvd, itunes to dvd

  36. 60
    person_guyz

    this is awesome. just what I have been looking for for ages! I’m going to have a go at donating to you (although last few times i’ve tried it doesn’t work as I am in China but from England so the donate process make my bank think I’m a fraudster..
    rn bsn online

  37. 59
    person_guyz

    Thank u very much for giving a superb script and i have a one doubt,
    clinical informatics

  38. Big props for this code!! It would prob help beginners if you mentioned that you can just outright save your code block as a searchform.php and leave it in the currently used template root (assuming it doesn’t have the file already)

  39. 57
    jibran

    Companies these days are using ergonomic furniture to ensure that there are no injuries from work related hazards. Use an ergonomic chair and feel better comfort in your back and shoulder. pension

  40. this is awesome. just what I have been looking for for ages! I’m going to have a go at donating to you (although last few times i’ve tried it doesn’t work as I am in China but from England so the donate process make my bank think I’m a fraudster..

  41. Best-of-Programm jedoch probiert Josef Hader etwas, was viele Kollegen machen, er aber normalerweise nicht: Nummern spielen und dazwischen Lieder singen. free articles and guide

  42. Best-of-Programm jedoch probiert Josef Hader etwas, was viele Kollegen machen, er aber normalerweise nicht: Nummern spielen und dazwischen Lieder singen.

  43. Nice sharing, thanks!

  44. thanks for the easy tutorial…implemented it perfectly on my website, looks awesome..

  45. I’m still having trouble with my search box..huhu

  46. for people like me, new to wordpress;
    a fantastic code…
    thankyou very much.

  47. Thank u very much for giving a superb script and i have a one doubt,
    how to add the script to twitter like social networks sites in the top of the wordpress themes.
    kindly sent the answer
    Thanks in advance,
    Ram

  48. thanks for the easy tutorial…implemented it perfectly on my website, looks awesome..!

  49. Great script, you just saved me a lot of work – thanks!

  50. 46
    Shaukat Ali

    Thanks really a very good script

  51. We supply all kinds of moncler boots,moncler bags,moncler coats,moncler jackets,moncler T-shirt,moncler vest and so on.We have got a good reputation pf our products with top quality and good price.We sincerely look forward to futher cooperation with you for mutual benefits.We are sure that you will find working with us is a pleasant, time-saving and profitable experience. Please feel free to contact us if you have any questions.Our website is http://www.newlyapparel.com.

  52. Hi I installed the code as displayed in a widget area, the search results work perfectly, but as you can see, the form shows the first line of the php code, not SEARCH.

    I created a file searchform.php and put it in the theme folder as suggsted. What is the code needed to to place those file contents in the widget area? I think that is the right question?!! cheers

  53. 43
    Rahul

    Good piece of code, useful when one don’t want a search button or don’t have space to do so. Thanks.

  54. nice one, exactly what i needed…

  55. 41
    Barry

    <3 this snippet! You rock DD.

  56. 40
    devbrat

    please send me code of search

  57. Perfect, thank you!

  58. Thank YA infonya……byponsel.com

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

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

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

  62. great script

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

  64. 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

  65. thanks a lot.. works fine

  66. 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.

  67. 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!

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

  69. 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.

  70. 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

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

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

  73. 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?

  74. 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]; }

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

  76. 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?

  77. 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?

  78. 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

  79. 17
    yogesh

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

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

    thanks !

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

  82. Thanks for this!

  83. 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?

  84. 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.

  85. 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 :)

  86. 10
    Jakob

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

  87. 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?

  88. 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.

  89. 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?

  90. matthijs: Thanks :)

  91. 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

  92. 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?

  93. 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.

  94. 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.

  95. 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.