Skip to content

Instantly share code, notes, and snippets.

@kaoru-fukusato
Created July 8, 2018 17:27
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 kaoru-fukusato/be5dfc43faed4506a22dc671568f2d39 to your computer and use it in GitHub Desktop.
Save kaoru-fukusato/be5dfc43faed4506a22dc671568f2d39 to your computer and use it in GitHub Desktop.
ユーザーが検索したワードをハイライト
//ユーザーが検索したワードをハイライト
function wps_highlight_results($text) {
if(is_search()){
$sr = get_query_var('s');
$keys = explode(" ",$sr);
$text = preg_replace('/('.implode('|', $keys) .')/iu', '<span class="searchhighlight">'.$sr.'</span>', $text);
}
return $text;
}
add_filter('the_title', 'wps_highlight_results');
add_filter('the_content', 'wps_highlight_results');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment