Skip to main contentSkip to footer

Problems with highlighting in post content

Versions 2.27.5 (Premium) and 4.24.4 (free) added extra security to highlighting search terms in post content. Before that, an attacker with contributor access could inject malicious content in the posts and the highlighting would then trigger that. This is now fixed, but the fix comes with a cost: the post content is passed through the wp_kses_post() function which strips out the malicious content, but also useful content.

The easy solution for these problems is to disable the “Highlight query terms in documents” setting. I don’t think it is all that useful in most cases, and all good browsers have an efficient in-page search feature that can be used to find the matching content.

Another option is to adjust the wp_kses_post() functionality with the wp_kses_allowed_html filter hook. If you need to allow CSS styles, for example, you can use this snippet:

add_filter( 'wp_kses_allowed_html', function( $html, $context ) {
  if ( 'post' == $context ) {
    $html['style'] = true;
  }
  return $html;
}, 10, 2 );

Note that this applies to everywhere on your site that uses the “post” context, not just these highlight cases.

If you have problems with a specific page, you can disable the highlighting feature on that page only:

add_action( 'wp_head', function() {
  if ( is_page( 123 ) ) {
    remove_filter( 'the_content', 'relevanssi_highlight_in_docs', 11 );
  }
} );

This checks the current page ID and if it’s 123, unhooks the filter function that adds the highlighting.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Related searches feature

…which have at least one matching token with the current search. Searching for “john” will match both “john blackbourn” and “john smith”. Searching for “john blackbourn” will match “john smith”, but won’t match “john”, because the function doesn’t suggest queries that consist of only one token. Also, only queries that…John Blackbourn wrote a cool related searches feature for Relevanssi Premium. It’s included from version 1.7.4. In order to use the related searches, you must have search logging enabled and a good log of past searches. Then add the following code to your search results template in a suitable place:…

Indexing attachment file names
Relevanssi has been working nicely for the normal usecase. But how does one setup indexing of attachment files. When someone…

Related Posts:

Currently there are no related posts available.

Comment Section:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed