Skip to content

Instantly share code, notes, and snippets.

@danilostrazzullo
Created January 9, 2017 10:49
Show Gist options
  • Save danilostrazzullo/2901a4fe0918fb6cd9b74363be4ec15d to your computer and use it in GitHub Desktop.
Save danilostrazzullo/2901a4fe0918fb6cd9b74363be4ec15d to your computer and use it in GitHub Desktop.
WP highlight search terms in results
<?php
// Add '<b>' tags to search terms in results
function namespace_highlight_results($text){
if ( is_search() && !is_admin() ){
$sr = get_query_var('s');
$keys = explode(" ",$sr);
$keys = array_filter($keys);
$text = preg_replace('/('.implode('|', $keys) .')/iu', '<b>'.$sr.'</b>', $text);
}
return $text;
}
add_filter('the_excerpt', 'namespace_highlight_results');
add_filter('the_title', 'namespace_highlight_results');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment