Remove WPBakery Page Builder (formerly Visual Composer) shortcodes from the Excerpt

2017 MacBook Pro Laptop showing blogpost with code

I’ve had this issue on a site where we’re using WPBakery Page Builder and its built-in “Grid Builder” for showing a selection of pages. Within the Grid Builder, we’re outputting the page excerpt. 

This means that WordPress shows certain shortcodes (or some variations of them) within the excerpt:

{{ vc_btn:title=Facebook&style=outline&shape=square&color=black&size=xs&i_icon_fontawesome=fa+fa-facebook&add_icon=true&link=url%3Ahttps%253A%252F%252Fwww.facebook.com }}

I’m not 100% sure why it’s that JSON type format instead of the standard shortcode.

For the time being, I wanted to remove it from all excerpts. This is what I added to functions.php to clean it up:

if(!function_exists('remove_wpbpb_from_excerpt'))  {
  function remove_wpbpb_from_excerpt( $excerpt ) {
    $patterns = array("/\[[\/]?vc_[^\]]*\]/","/\{[\{] ?vc_[^\}]*\}\}/");
    $replacements = "";
    $clean_excerpt = preg_replace($patterns, $replacements, $excerpt);
    return $clean_excerpt;
  }
}
add_filter( 'the_excerpt', 'remove_wpbpb_from_excerpt' , 11, 1 );

Note: the filter priority makes a difference here. Using 10 will not work. 11 is the lowest that worked with our setup.

This simply replaces all shortcodes starting with vc_ as well as the text that uses the {{ syntax and includes vc_ within the brackets.

Leave a Reply