I found this function on the net. I have placed this in my custom functions file separate from functions.php.
function highlight_search_term($text){
if(is_search()){
$keys = implode('|', explode(' ', get_search_query()));
$text = preg_replace('/(' . $keys .')/iu', '<span class="search-term">\1</span>', $text);
}
return $text;
}
add_filter('the_excerpt', 'highlight_search_term');
add_filter('the_title', 'highlight_search_term');
It works fine except it alters the read more link. Hovering over this link shows:
http://www.mysite.com/%3span%20classs=
When it is removed the link is unaffected. What is wrong with it? I have used the following CSS with it:
.search-term {
background: yellow;
}
You can try running the function early so that it runs before the
more
link is created.But I don’t think that solves the deeper problem, even if it were to work. That function is pretty crude and it will replace inside URLs and inside tag attributes, which it should not ever be doing as it would result, almost certainly, in broken links and broken markup.
Here is a more complicated, minimally tested version:
I built this to operate piecemeal, so that it (hopefully) will not replace anything at all inside of
<
and>
tag opening and closing markers. It moves the markup out of the way, replaces everything else, and then puts the markup back. I am sure it could be cleaned up but that is a start.