Skip to content

Instantly share code, notes, and snippets.

@JAW-Dev
Last active August 21, 2022 23:08
Show Gist options
  • Save JAW-Dev/5745048 to your computer and use it in GitHub Desktop.
Save JAW-Dev/5745048 to your computer and use it in GitHub Desktop.
Highlight WordPress Search Terms
// 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