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!

Pages: « 7 6 5 [4] 3 2 1 » Show All

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

  2. 59
    person_guyz

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

  3. 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)

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

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

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

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

  8. Nice sharing, thanks!

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

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

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

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

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

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

  15. 46
    Shaukat Ali

    Thanks really a very good script

Pages: « 7 6 5 [4] 3 2 1 » Show All

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.