Last active
August 21, 2022 23:08
-
-
Save JAW-Dev/5745048 to your computer and use it in GitHub Desktop.
Highlight WordPress Search Terms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// source: http://bradsknutson.com/blog/highlight-search-terms-wordpress/ | |
function highlight_search_term($text){ | |
if(is_search()){ | |
$keys = implode('|', explode(' ', get_search_query())); | |
$text = preg_replace('/(' . $keys .')/iu', '<span class="search-term">\0</span>', $text); | |
} | |
return $text; | |
} | |
add_filter('the_excerpt', 'highlight_search_term'); | |
add_filter('the_title', 'highlight_search_term'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment