How to create a custom search form in WordPress

By Posted in - Tutorials on September 26th, 2013 6 Comments

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.

(6) awesome folk have had something to say...

  • baidyanath - Reply

    January 16, 2014 at 5:30 am

    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.

    • Rob Golbeck - Reply

      January 18, 2014 at 2:22 pm

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

  • Sagive - Reply

    February 15, 2014 at 2:51 pm

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

    • Rob Golbeck - Reply

      February 15, 2014 at 3:02 pm

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

  • Emre - Reply

    March 31, 2014 at 9:28 am

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

    • Rob Golbeck - Reply

      March 31, 2014 at 11:15 am

      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!

Please leave a Comment