Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Created December 3, 2020 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgibbs189/8df52bf40e1a974408aba23be1a7c0a3 to your computer and use it in GitHub Desktop.
Save mgibbs189/8df52bf40e1a974408aba23be1a7c0a3 to your computer and use it in GitHub Desktop.
FacetWP - highlight template text containing search term
(function($) {
FWP.hooks.addAction('facetwp/loaded', function() {
var facet_name = 'search'; // EDIT ME
var allowed_elements = '.cat-item__title a, .cat-item__descr p'; // EDIT ME
var keywords = FWP.facets[facet_name].toString();
if ('' !== keywords) {
$(allowed_elements).each(function() {
var results = $(this).html();
results = results.replace(new RegExp(keywords, "gi"), (match) => `<mark>${match}</mark>`);
$(this).html(results);
});
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment