Fix Visual Composer shortcode display in Relevanssi search result
If you use Visual Composer to build/design your pages, posts or custom post types, and use Relevanssi – A Better Search to replace the default WordPress search with some extra feature, you will see search results are coming up with visual composer shortcodes visible.
By default, Relevanssi skips most of the Visual Composer shortcodes. But some are still visible. Following code will fix that problem.
/**
* Fix Visual Composer shortcode display in Relevanssi search result
* @param $content
* @return string|string[]|null
*/
function careless_vc_filtered_content( $content ) {
$content = preg_replace('/\[\/?vc_.*?\]/', '', $content);
$content = preg_replace( '/\[vc_.*?text="(.*?)".*\]/im', '', $content );
$content = preg_replace( '#\[[^\]]+\]#', '', $content );
return $content;
}
add_filter( 'relevanssi_pre_excerpt_content', 'careless_vc_filtered_content', 999 );
add_filter( 'relevanssi_post_content', 'careless_vc_filtered_content', 999 );
Above code will remove all Visual Composer shortcodes from search results and will show only plain content.
Code goes in the functions.php
file of your active theme.