Menu Close

WooCommerce: Customize Product Search Form HTML

default

The “Search products…” WooCommerce form usually appears in the website header and/or the Shop page sidebar based on your theme settings and widget setup.

It usually features an input form with a magnifying glass icon (unless your theme customizes it via CSS), without a visible search button, and on enter it redirects users to the search result page.

Case study: most WooCommerce Ajax Search plugins target the WooCommerce product search form input, so that when you start typing, the “instant search results” window pops up. However, I needed to disable this behavior on certain subsites/languages and the only workaround here was to add a class to the search form input, so that I could target it in my Ajax plugin selector exclusions.

Even if the above scenario is very unlikely, the challenge is still valid. So, how do we customize the HTML of the “Search products…” WooCommerce form (so, not the CSS, but the actual HTML output)? Let’s go!

The default WooCommerce Product Search form HTML is the following (as per woocommerce/templates/product-searchform.php template):

<form role="search" method="get" class="woocommerce-product-search" action="<?php echo esc_url( home_url( '/' ) ); ?>">
	<label class="screen-reader-text" for="woocommerce-product-search-field-<?php echo isset( $index ) ? absint( $index ) : 0; ?>"><?php esc_html_e( 'Search for:', 'woocommerce' ); ?></label>
	<input type="search" id="woocommerce-product-search-field-<?php echo isset( $index ) ? absint( $index ) : 0; ?>" class="search-field" placeholder="<?php echo esc_attr__( 'Search products&hellip;', 'woocommerce' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />
	<button type="submit" value="<?php echo esc_attr_x( 'Search', 'submit button', 'woocommerce' ); ?>" class="<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ); ?>"><?php echo esc_html_x( 'Search', 'submit button', 'woocommerce' ); ?></button>
	<input type="hidden" name="post_type" value="product" />
</form>

And it looks like this (top right in the Storefront theme header, for example):

PHP Snippet: Edit WooCommerce Product Search Form HTML

Note: based on the HTML output above, a filter exists, called “get_product_search_form”, which allows us to override the whole HTML, or parts of it.

In the example below, I’m simply adding a new class by “replacing” some HTML with the str_replace() PHP function. You can of course recreate the form HTML from scratch and return that via the filter instead!

/**
 * @snippet       Add class @ WooCommerce Product Search Form
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'get_product_search_form' , 'bbloomer_custom_product_searchform' );

function bbloomer_custom_product_searchform( $html ) {
	return str_replace( 'class="search-field', 'class="additional-class search-field', $html );
}
View Source
Posted in WooCommerce Tips