Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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();

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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