The generate_navigation_search_output
filter allows you to change the HTML markup of your navigation search form.
Usage
WooCommerce Products
For example, if we want our navigation search form to search WooCommerce products:
add_filter( 'generate_navigation_search_output', function() {
printf(
'<form method="get" class="search-form navigation-search" action="%1$s">
<input type="search" class="search-field" value="%2$s" name="s" title="%3$s" />
<input type="hidden" name="post_type" value="product" />
</form>',
esc_url( home_url( '/' ) ),
esc_attr( get_search_query() ),
esc_attr_x( 'Search', 'label', 'generatepress' )
);
} );
Posts
If we want our navigation search form to search Posts only:
add_filter( 'generate_navigation_search_output', function() { printf( '<form method="get" class="search-form navigation-search" action="%1$s"> <input type="search" class="search-field" value="%2$s" name="s" title="%3$s" /> <input type="hidden" name="post_type" value="post" /> </form>', esc_url( home_url( '/' ) ), esc_attr( get_search_query() ), esc_attr_x( 'Search', 'label', 'generatepress' ) ); } );
Add a Placeholder
Or, if you want to add a placeholder to the input:
add_filter( 'generate_navigation_search_output', function() {
printf(
'<form method="get" class="search-form navigation-search" action="%1$s">
<input type="search" placeholder="Enter your search" class="search-field" value="%2$s" name="s" title="%3$s" />
</form>',
esc_url( home_url( '/' ) ),
esc_attr( get_search_query() ),
esc_attr_x( 'Search', 'label', 'generatepress' )
);
} );
Clear Search Field
We can also use the snippet below to clear the search field after search is completed:
add_filter( 'generate_navigation_search_output', function() { printf( // WPCS: XSS ok, sanitization ok. '<form method="get" class="search-form navigation-search" action="%1$s"> <input type="search" class="search-field" value="" name="s" title="%2$s" /> </form>', esc_url( home_url( '/' ) ), esc_attr_x( 'Search', 'label', 'generatepress' ) ); } );