Posted on

Visual Composer: Indexing Raw HTML elements

Visual Composer and WP Bakery have a Raw HTML element that can be used to add HTML code on your pages. By default, this content is stored in an encoded format Relevanssi can’t read. Fortunately the encoding is simple Base64 encoding that is easy to read.

So, in order to read the contents of the Raw HTML element, all it takes is a custom function that finds the encoded content, decodes it and adds it to the post so Relevanssi can see it. We can use the relevanssi_post_content filter hook to modify the post content. Add this function to your theme functions.php or in a code snippet:

add_filter( 'relevanssi_post_content', 'rlv_vc_raw_html', 8 );
function rlv_vc_raw_html( string $content ) : string {
  if ( preg_match_all( '/\[vc_raw_html\](.*?)\[\/vc_raw_html\]/', $content, $matches ) > 0 ) {
    foreach ( $matches[1] as $encoded_data ) {
      $raw_html = urldecode( base64_decode( $encoded_data ) );
      $content .= ' ' . $raw_html;
    }
  }
  return $content;
}

This function needs to run on a priority lower than 9, because on priority 9 Relevanssi runs the relevanssi_remove_page_builder_shortcodes() function that removes page builder shortcodes and in the case of [vc_raw_html] also removes the encoded content.

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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.