0

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?

1 Answer 1

0

Usually, for filters like these content is passed into the filters, processed and passed back out. That is more or less the definition of a filter. Of course that isn't strictly true. You don't have to take input and you can wipe out "input" content if you want, though it is probably not a good idea most of the time.

function search_excerpt_highlight($excerpt) {
    $keys = implode('|', explode(' ', get_search_query()));
    $excerpt = preg_replace('/(' . $keys .')/iu', '<span class="search-highlight">\0</span>', $excerpt);
    echo '<p>' . $excerpt . '</p>';
}

The way those functions are written they would wipe out any filter processing that executed earlier in the queue. That is, filters are added to a kind of execution stack and then applied one by one. Anything earlier before those filters would be lost since they aren't accepting information, the are just grabbing the raw data with $excerpt = get_the_excerpt(); and $title = get_the_title();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.