Created
July 8, 2018 17:27
-
-
Save kaoru-fukusato/be5dfc43faed4506a22dc671568f2d39 to your computer and use it in GitHub Desktop.
ユーザーが検索したワードをハイライト
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
//ユーザーが検索したワードをハイライト | |
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