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.
(Code updated March 23, 2016)
<form role="search" method="get" class="searchform group" action="<?php echo home_url( '/' ); ?>"> <label> <span class="offscreen"><?php echo _x( 'Search for:', 'label' ) ?></span> <input type="search" class="search-field" placeholder="<?php echo esc_attr_x( 'Search', 'placeholder' ) ?>" value="<?php echo get_search_query() ?>" name="s" title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" /> </label> <input type="image" alt="Submit search query" src="<?php echo get_template_directory_uri(); ?>/images/search-icon.png"> </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.