Created
January 9, 2017 10:49
-
-
Save danilostrazzullo/2901a4fe0918fb6cd9b74363be4ec15d to your computer and use it in GitHub Desktop.
WP highlight search terms in results
This file contains hidden or 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
<?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