How to create a custom search form in WordPress

You can display a search form anywhere on a WordPress site by including <?php get_search_form(); ?> in the template. If your theme contains searchform.php, WordPress will display the contents of the file. If the theme does not contain searchform.php, WordPress will default to the built-in search form instead.

The built-in search is fine if you just want a basic form. But what if you want to, say, use an image for the submit button, or change the placeholder text?

I frequently come across themes where the developer has hacked the built-in form to make custom changes rather than using searchform.php. When I first started developing WordPress themes that’s how I did it too. But it’s a rookie mistake.

The problem is, when you hack the built-in search form, every time the WordPress core gets updated your changes will be overwritten. You’ll have to go back and make your changes again, if you remember to do so. And even then you’ll need to remember what you did to change it in the first place.

This is one of the reasons you should never touch the WordPress core. All your custom code should stay inside your theme directory.

For site search, searchform.php is used for just that.

Creating a custom search form in WordPress is super easy. Just create a new php file called searchform.php and save it in your theme directory. Then add your form code to the file.

For example:

WordPress will now draw on this code to display your search form rather than the built-in form. You can customize it any way you want, and it won’t be affected by WordPress core updates.

See the Codex for more about adding a search form to your WordPress site.

25 thoughts

  1. baidyanath says:

    Explained in very clear manner, to the point. Thank you I was looking for the customizing the default form, not able understand properly in wordpress,org.

    1. Rob Golbeck says:

      Thanks for your comment Baidyanath! I’m glad this helped! :)

  2. Sagive says:

    Just remmember to use “searchsubmit” in the id instead of “search submit” meaning no spaces there…

    1. Rob Golbeck says:

      Fixed. Good catch – thanks for pointing it out! Not sure how I missed that….

  3. Emre says:

    Hi,
    I have a question and I couldn’t get a solution since last month.
    I have lost of categories, pages and re-directions in my blog so my search box finds many unnecessary results if we you try to search something. I want to customize my search.php for only categories part. In other words, we you search something, the results should be only from categories sections. So I will be get rid of redundant & duplicated results. My current codes are as below…Please help me :)

    1. Rob Golbeck says:

      Hi EMRE, Thanks for your comment and question :)

      I didn’t see your code, however I think what you’re looking to do should be possible with an advanced search plugin. The plugin I’m most familiar with is Relevanssi. It has an option to show only categories (or whatever other post type or taxonomy you choose) in the search results.

      I hope that helps!

  4. kapi says:

    This helps a lot

  5. Jonathan says:

    Good afternoon. Is there potential here to modify this so that the search is carried out on an OpenCart installation rather than the WordPress site? Stab in the dark I know but I am hunting for a solution to create a search box which can search in OpenCart and return those results in OpenCart.

    1. Rob Golbeck says:

      Hi Jonathan,

      I really couldn’t say, as I’m not familiar with OpenCart. You’ll have to ask the OpenCart support folks. This post is focused on the WordPress search function.

  6. Jonathan says:

    No problem, Rob. It was a stab in the dark and I appreciate you replying.

  7. Faisal says:

    i have a search field in header right side please tell me how its work with custom coding.

  8. Mark says:

    Thanks for this easy to understand and very helpful article.

    I am working to set this up for a library website, and I would like to add one bit of functionality beyond what you have described.

    We need to add a radio box with two options
    A.) search this site – the normal wordpress search
    B.) post the search to our catalog website

    Is there an easy way to make the form post to either option based on the user’s radio button selection?

    Thanks

    1. Rob Golbeck says:

      Hi Mark,
      That’s an excellent question. I actually come from a library background myself, so I’m curious how to make this work too.

      Are you using custom post types for the catalogue records? If so, this tutorial should help: http://www.wpbeginner.com/wp-tutorials/how-to-create-advanced-search-form-in-wordpress-for-custom-post-types/.

      Haven’t had a chance to try it out yet, but it looks like it should do the trick.

      Let me know how it goes!

  9. Nera Studio says:

    The best, thank you so much for you tutorial works as charmi, thank in advanced…

  10. rohit says:

    hello sir….

    I want to make a search form as corephp:
    e.g:
    1st step: User choose category from drop-down .
    2nd step: Enter keywords .
    3rd step: Search this “keyword” from selected “category” not from whole site.

    Please suggets me .

    Thanks in advance.

  11. Aji Wibowo says:

    Thanks! I am developing a new website for my new project, and get confused with this simple matter! Thanks for pointing that out, clear and to the point. Really appreciated that man.

    Cheers,
    Aji

  12. Narwinder says:

    On my website, i want to put a search option by location so that when someone search my website, they can find results from a specific location i.e. a specific city. Can you help me with that?

  13. Richard says:

    Sweet! worked for me…

  14. nazmul says:

    Hi i follow this snippest. This is 100% work . Thanks for share

  15. Chris B says:

    Nice work Rob – I came upon this from a Google search and used a bit of your code.

    1. Rob says:

      Thanks Chris! Glad you found it useful. :)

  16. Really nice 100% work thanks

  17. Hey There. I found your blog using msn. This is a really well written article.
    I’ll be sure to bookmark it and return to read more of your
    useful info. Thanks for the post. I will definitely comeback.

  18. christi says:

    I am trying to make a custom search bar on the home page of wordpress site for online courses and offline classes center.. see more details here

    http://monosnap.com/image/m8wRA1GOPgyveMOwQ6ZCPFC1DB8HtB#

    Is there any plugin or make this kind of search bar or any codes I have to put in for creating custom search bar like this on page on either on slider or home page.

  19. Fine, simple and useful, thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">