- Help Center Home
- Getting started
- Introducing FacetWP
- Installation and updates
- FAQ
- How it works
- What are facets?
- Facet types
- Built-in facet types
- Checkboxes
- Dropdown
- Radio
- fSelect
- Hierarchy
- Slider
- Search
- Autocomplete
- Date Range
- Number Range
- Star Rating
- Proximity
- Pager + load more
- Sort
- Reset
- User Selections
- Add-on facet types
- Map
- Hierarchy Select
- Range List
- Time Since
- A-Z Listing
- Color
- Custom facet types
- Custom facet types
- Indexing
- Listing templates
- Extras & integrations
- Add-on features and extras
- Using FacetWP with …
- Built-in integrations
- Advanced Custom Fields
- WooCommerce
- SearchWP
- WP-CLI
- Add-on integrations
- Blocks
- Bricks
- Elementor
- Beaver Builder
- WP Recipe Maker and Tasty Recipes
- Relevanssi
- WPML and Polylang
- Meta Box
- Flatsome (theme)
- External integrations
- Breakdance
- Document Library Pro
- Listify (theme)
- Listable (theme)
- WPGraphQL
- Tips & tricks
- WordPress multi-site
- WP All Import
- WebToffee Import Export
- WP Job Manager
- Easy Digital Downloads
- EDD Reviews
- Intuitive Custom Post Order
- Custom Taxonomy Order
- Post Types Order
- Genesis framework
- WP External Links
- ElasticPress
- Yoast SEO
- All in One SEO (Pro)
- The Events Calendar Pro
- Google Analytics 4
- Image Optimization by Optimole
- Meow Lightbox
- Cookiebot
- Caching, hosting & security
- Object caching
- WP Rocket
- Cloudflare
- WP Engine
- Pressable
- New Relic
- WordPress REST API Authentication
- All-In-One Security (AIOS)
- Fast Velocity Minify
- Incompatibilities
- Incompatible plugins and themes
- Troubleshooting
- Troubleshooting guide
- Using the right query
- Common issues
- Common indexing issues
- Get support
- Developers
- Hooks reference
- Indexing hooks
- Querying hooks
- Output hooks
- facetwp_facet_display_value
- facetwp_facet_html
- facetwp_facet_render_args
- facetwp_facet_pager_link
- facetwp_facet_sort_options
- facetwp_template_html
- facetwp_shortcode_html
- facetwp_render_params
- facetwp_render_output
- facetwp_builder_item_value
- facetwp_builder_dynamic_tags
- facetwp_builder_dynamic_tag_value
- Advanced hooks
- Deprecated hooks
- JavaScript reference
- Shortcodes reference
- FacetWP REST API
- How FacetWP works
- The FacetWP URL
- FacetWP speed and limits
- Tutorials
- Code snippets
- Feedback
- What’s new
- Changelog
- News & announcements
How to add a “Clear” button to a Search facet
Currently, 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:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_action( 'facetwp_scripts', function() { ?> <script> (function($) { $(document).on('facetwp-loaded', function() { var searchfacet = 'my_search_facet'; // Replace 'my_search_facet' with the name of your Search facet var searchbox = $('[data-name="' + searchfacet + '"] .facetwp-search'); if(! searchbox.next('i').length) { searchbox.after('<i class="clear" title="Clear Search"></i>'); } if (searchbox.val() === '') { searchbox.next().hide(); } searchbox.on('keyup', function() { if('yes' === FWP.settings[searchfacet]['auto_refresh']) { $(this).addClass('loading'); } if ($(this).val() !== '') { $(this).next().show(); } }); searchbox.removeClass('loading'); searchbox.next().click(function() { // ignore while Search facet is loading if (!searchbox.prev().hasClass('f-loading')) { $(this).hide(); searchbox.val(''); if (FWP.facets[searchfacet].length) { FWP.autoload(); } } }); }); })(jQuery); </script> <?php }, 100 );
Also, add the following CSS to your (child) theme’s functions.php. In this example, we are using an ‘x’ image already present in FacetWP (in line 15), but you can use any other (font) icon.
The CSS also hides the icon when the facet is loading and when an auto-refresh is ongoing:
How to use custom PHP code?
PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info
add_action( 'wp_head', function() { ?> <style> .facetwp-type-search i.clear { position: absolute; right: 30px; height: 100%; line-height: 1; cursor: pointer; display: inline-block; opacity: 0.3; width: 24px; height: 100%; background: transparent url('/wp-content/plugins/facetwp/assets/images/icon-close.png') 6px 50% no-repeat; background-size: 14px 14px; } .facetwp-type-search i.clear:hover { opacity: 0.8; } /* no clear icon while the Search facet is loading */ /* and when an auto-refresh is running */ .facetwp-type-search .f-loading + input + i.clear, .facetwp-type-search .loading + i.clear { display: none !important; } .facetwp-facet input.facetwp-search { padding-right: 54px; } </style> <?php }, 100 );
See also
- The Search facet type
- Using FacetWP with SearchWP
- Using FacetWP with Relevanssi
- Add search suggestions below a Search facet
- Redirect a search box to a FacetWP results page
Last updated: September 10, 2024