I found the following two functions in order to highlight the current search term on my search-results page.
function search_excerpt_highlight() {
$excerpt = get_the_excerpt();
$keys = implode('|', explode(' ', get_search_query()));
$excerpt = preg_replace('/(' . $keys .')/iu', '<span class="search-highlight">\0</span>', $excerpt);
echo '<p>' . $excerpt . '</p>';
}
function search_title_highlight() {
$title = get_the_title();
$keys = implode('|', explode(' ', get_search_query()));
$title = preg_replace('/(' . $keys .')/iu', '<span class="search-highlight">\0</span>', $title);
echo $title;
}
When using this, other filters that are added to the_excerpt()
are disabled, like e.g. the custom-more-link I added.
Now there is plain HTML in my search-results like this whatever there is class="more-link">
Any ideas how to fix that?