How to add a clear input icon to a Search facetCurrently, Search facets do not have a “Clear” button or icon.

The following code adds an ‘x’ clear button/icon to the search input box of a Search facet. It will automatically reload the (other) facets when clearing the search box, but only if necessary: if a previous search is being cleared.

The code takes the Search facet’s “Auto refresh” setting into account, which makes the result automatically refresh while typing (on keyup, after a slight delay). If this setting is enabled, the clear icon will only appear after the refresh, to prevent users from clearing a running auto-refresh.

Add the following code to your (child) theme’s functions.php. Make sure to replace my_search_facet with the name of your Search facet:

[clear-search-php]

Also, add the following CSS to your (child) theme’s style.css file. In this example, we are using an ‘x’ image already present in FacetWP, but you could use any other (font) icon.

The CSS also hides the icon when the facet is loading and when an auto-refresh is ongoing:

[clear-search-css]