The default WordPress search form is functional — but it’s rarely beautiful, and it’s even less flexible. Whether you want to match your theme’s design, search specific post types, or add extra filters, building a WordPress custom search form is easier than you might think.
In this guide, I’ll walk you through three proven methods — from the simplest to the most customizable — so you can pick the one that fits your project.
Table of Contents
- Why Customize the WordPress Search Form?
- Method 1: The searchform.php Template File
- Method 2: The get_search_form Filter Hook
- Method 3: Plugin Method
- Bonus: Searching Custom Post Types
- Styling Your Custom Search Form with CSS
- Conclusion
Why Customize the WordPress Search Form?
Before jumping into the code, let’s talk about why you’d want a custom search form in the first place.
The built-in WordPress search form has a few notable limitations:
- It only searches posts and pages by default. If your site uses custom post types (products, courses, listings, etc.), they may be excluded from search results.
- It has zero styling. The default form inherits very basic browser styles, which rarely matches your theme.
- You can’t add filters or dropdowns. Want to let users search within a specific category or post type? Not possible without customization.
- Placeholder text is non-existent. The default form has no placeholder, making it feel unpolished.
Fortunately, WordPress gives you full control over the search form via a clean template hierarchy and a set of hooks. Let’s dig in.
Method 1: The searchform.php Template File
The simplest and most theme-friendly way to create a WordPress custom search form is to create a file called searchform.php in your active theme’s directory.
When WordPress renders a search form — whether via the get_search_form() function, the Search widget, or a shortcode — it first checks if searchform.php exists in your theme. If it does, WordPress uses your custom template instead of the default one.
How to Create searchform.php
- Open your theme directory (typically
/wp-content/themes/your-theme/). - Create a new file named
searchform.php. - Add the following code:
<?php /* Custom WordPress Search Form */ ?>
<form role="search" method="get" id="searchform" class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<div class="search-wrapper">
<label class="screen-reader-text" for="s">
<?php echo _x( 'Search for:', 'label' ); ?>
</label>
<input
type="search"
id="s"
name="s"
class="search-field"
placeholder="<?php echo esc_attr_x( 'Search...', 'placeholder' ); ?>"
value="<?php echo get_search_query(); ?>"
/>
<button type="submit" id="searchsubmit" class="search-submit">
<?php echo esc_attr_x( 'Search', 'submit button' ); ?>
</button>
</div>
</form>Key Rules to Follow
There are a few things you must keep in mind when building a custom search form template:
- The form method must be
GET— not POST. WordPress search relies on the query string. - The action must point to
home_url('/')— this is what triggers the search results page. - The text input must use
name="s"— WordPress reads the search query from this parameter. - Always use
get_search_query()to pre-fill the field value. This prevents XSS vulnerabilities. - Include a screen-reader label for accessibility — even if it’s visually hidden via CSS.
Once you save the file, any call to get_search_form() across your theme will automatically use your custom template.
Method 2: The get_search_form Filter Hook
If you’re building a plugin, working with a child theme, or simply prefer keeping everything in functions.php, the get_search_form filter is your best option.
This approach lets you override the search form HTML using a WordPress filter, without creating a separate template file.
Basic Implementation
Add this to your theme’s functions.php file or a custom plugin:
function andriy_custom_search_form( $form ) {
$form = '<form role="search" method="get" id="searchform" class="searchform" action="' . esc_url( home_url( '/' ) ) . '">
<div class="search-wrapper">
<label class="screen-reader-text" for="s">' . _x( 'Search for:', 'label' ) . '</label>
<input
type="search"
id="s"
name="s"
class="search-field"
placeholder="' . esc_attr_x( 'Search...', 'placeholder' ) . '"
value="' . get_search_query() . '"
/>
<input
type="submit"
id="searchsubmit"
class="search-submit"
value="' . esc_attr_x( 'Search', 'submit button' ) . '"
/>
</div>
</form>';
return $form;
}
add_filter( 'get_search_form', 'andriy_custom_search_form', 40 );The priority 40 ensures your filter runs after any other filters that might modify the form.
Note: The
get_search_formfilter runs even if asearchform.phpfile exists in your theme. If both are present, the filter takes precedence.
Method 3: Plugin Method
Not comfortable editing theme files? A plugin is a solid no-code alternative for building a WordPress custom search form.
A few options worth considering:
- SearchWP — The most powerful search plugin for WordPress. It allows you to search custom post types, custom fields, WooCommerce products, PDFs, and more. Comes with a form widget and shortcode.
- Relevanssi — A free plugin that replaces the default WordPress search with a more relevant, fuzzy-match algorithm. Integrates seamlessly with existing search forms.
- FacetWP — Best for building filterable search forms with dropdowns, checkboxes, and sliders. Great for e-commerce or directory sites.
For most WordPress blogs, Relevanssi is a great starting point because it’s free, improves search quality out of the box, and doesn’t require changes to your existing forms.
Bonus: Searching Custom Post Types
By default, the WordPress search only looks at standard posts and pages. If your site has custom post types — like product, course, or listing — you’ll need to explicitly include them.
The easiest way is to add hidden inputs to your search form:
<form role="search" method="get" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<input type="search" name="s" value="<?php echo get_search_query(); ?>" />
<!-- Include specific post types in search results -->
<input type="hidden" name="post_type[]" value="post" />
<input type="hidden" name="post_type[]" value="course" />
<input type="hidden" name="post_type[]" value="product" />
<input type="submit" value="Search" />
</form>Each post_type[] hidden input tells WordPress which post types to include in the results. You can add as many as needed.
Alternatively, you can handle this server-side with the pre_get_posts hook in functions.php, which keeps the form clean:
function andriy_include_cpt_in_search( $query ) {
if ( $query->is_search() && ! is_admin() && $query->is_main_query() ) {
$query->set( 'post_type', [ 'post', 'course', 'product' ] );
}
}
add_action( 'pre_get_posts', 'andriy_include_cpt_in_search' );This approach is cleaner and works regardless of which search form is used on the site.
Styling Your Custom Search Form with CSS
Once your custom search form is in place, you’ll want to style it to match your theme. Here’s a clean CSS starting point that works with the code snippets above:
/* Search Form Wrapper */.searchform .search-wrapper {
display: flex;
align-items: center;
gap: 8px;
max-width: 500px;
}
/* Search Input */.searchform .search-field {
flex: 1;
padding: 10px 16px;
font-size: 16px;
border: 2px solid #ddd;
border-radius: 4px;
outline: none;
transition: border-color 0.2s ease;
}
.searchform .search-field:focus {
border-color: #0073aa;
}
/* Submit Button */.searchform .search-submit {
padding: 10px 20px;
background-color: #0073aa;
color: #fff;
font-size: 16px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.2s ease;
}
.searchform .search-submit:hover {
background-color: #005d8c;
}Customize the colors, font sizes, and border-radius to match your own theme’s design system.
Conclusion
Building a WordPress custom search form is one of those small wins that can make a big difference — both in user experience and in the overall polish of your site.
Here’s a quick recap of the three methods:
- searchform.php — Best for theme developers who want a clean, template-based solution.
- get_search_form filter — Best for plugins or child themes that need to override the form via PHP.
- Plugin — Best for non-developers or sites that need advanced search features without custom code.
Whichever approach you choose, make sure to always use get_search_query() to sanitize the search field value, and always set method="get" with the form action pointing to home_url('/').
If you’d like help implementing any of these methods on your WordPress site, feel free to reach out — I’m always happy to help.

