KrisChase.com

Remove Visual Composer from WordPress Excerpt

Posted in Web Development on 06.10.2015 by @chasebadkids

If you’ve ever been in a situation where you’re using Visual Composer within a site and you need to pull in an excerpt in a way that doesn’t include any shortcodes you can use the below functions to make that happen.

All you need to do is drop the following code into your functions.php file:

The Code for removing Visual Composer

/** add this to your function.php child theme to remove ugly shortcode on excerpt */

if(!function_exists('remove_vc_from_excerpt'))  {
  function remove_vc_from_excerpt( $excerpt ) {
    $patterns = "/\[[\/]?vc_[^\]]*\]/";
    $replacements = "";
    return preg_replace($patterns, $replacements, $excerpt);
  }
}

/** * Original elision function mod by Paolo Rudelli */

if(!function_exists('kc_excerpt')) {

/** Function that cuts post excerpt to the number of word based on previosly set global * variable $word_count, which is defined below */

  function kc_excerpt($excerpt_length = 20) {

    global $word_count, $post;

    $word_count = isset($word_count) && $word_count != "" ? $word_count : $excerpt_length;

    $post_excerpt = $post->post_excerpt != "" ? $post->post_excerpt : strip_tags($post->post_content); $clean_excerpt = strpos($post_excerpt, '...') ? strstr($post_excerpt, '...', true) : $post_excerpt;

    /** add by PR */

    $clean_excerpt = strip_shortcodes(remove_vc_from_excerpt($clean_excerpt));

    /** end PR mod */

    $excerpt_word_array = explode (' ',$clean_excerpt);

    $excerpt_word_array = array_slice ($excerpt_word_array, 0, $word_count);

    $excerpt = implode (' ', $excerpt_word_array).'...'; echo ''.$excerpt.'';

  }

}

After you insert the previous code into your functions.php file, you can call the function wherever it’s needed using the following:

Note:

You can also pass in an argument to the function which will allow you to modify the length of your excerpt while excluding those Visual Composer shortcodes.

Is there any way you see this function for removing Visual Composer Shortcodes from WordPress could be improved? If so, shoot me an email and I’ll work on getting it added!

Menu