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.
<form action="<?php bloginfo('siteurl'); ?>" id="searchform" method="get"> <fieldset> <label for="s" class="screen-reader-text">Search for:</label> <input type="search" id="s" name="s" placeholder="Enter keywords" required /> <input type="image" id="searchsubmit" alt="Search" src="<?php bloginfo( 'template_url' ); ?>/images/searchicon.png" /> </fieldset> </form>
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.