Posted on

Relevanssi breaks autoembeds on result pages

You may notice that enabling Relevanssi custom excerpts breaks all autoembeds (things like Youtube video embeds) on search results pages. That’s done on purpose, because the autodiscovery of those embeds can be a proper performance killer if the search results include lots of embedded content. Thus, Relevanssi switches the feature off, considering it’s not commonly used on search results pages anyway.

But what if you do need it? There are three solutions for this, and choosing the right one depends on the circumstances:

  1. Disable Relevanssi custom excerpts.
  2. Remove the Relevanssi autoembed disabler.
  3. Don’t rely on the_content filter hook and instead use autoembed directly.

In general I recommend the option 3.

Disable Relevanssi custom excerpts

This one’s straightforward. Just disable Relevanssi custom excerpts, and this will no longer be a problem. Of course, you won’t get Relevanssi custom excerpts, either.

Remove the Relevanssi autoembed disabler

From the versions 2.14.4 (Premium) and 4.12.4 (free) onwards you can allow autoembed discovery with this line of code:

add_action( 'init', function() {
  remove_action( 'relevanssi_pre_the_content', 'relevanssi_kill_autoembed' );
}, 99 );

This will allow the autoembeds to work on the_content filter hook again. However, this may lead to performance problems. Try it out and see what happens. If the search becomes too slow, look for the next approach.

Use autoembed directly

Using apply_filters( 'the_content', $content_with_embed ) to do the autoembeds is not the ideal way to approach this in the first place. It does get the job done, but may have unwanted side effects for example from other plugins that do something with the_content hook. I think it’s better to use the autoembed functionality directly.

That’s easy. Instead of

echo apply_filters( 'the_content', $content_with_embed );

you do this:

$embed = new WP_Embed();
echo $embed->autoembed( $content_with_embed );

This will do the autoembedding without any side effects and with no interference from Relevanssi.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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