How to strip all visual composer shortcode/tags from wordpress's post_content fetched with custom query

14,889

Solution 1

Here, you can try and easily add some short codes in array that you needs and also you can remove all shortcodes via below code.

$the_content = '[VC_ROW][VC_COLUMN]some text1[/VC_COLUMN] etc.[/VC_ROW][VC_COLUMN_INNTER width="1/3"][/VC_COLUMN_INNTER]';

$shortcode_tags = array('VC_COLUMN_INNTER');
$values = array_values( $shortcode_tags );
$exclude_codes  = implode( '|', $values );

// strip all shortcodes but keep content
// $the_content = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $the_content);

// strip all shortcodes except $exclude_codes and keep all content
$the_content = preg_replace( "~(?:\[/?)(?!(?:$exclude_codes))[^/\]]+/?\]~s", '', $the_content );
echo $the_content;

you want to remain some shortcodes you can't use strip_shortcodes() for that.

Solution 2

I want to remove/strip all these shortcode from the content and retrieve only plain text from it.

Solution that worked for me:

$content = strip_tags( do_shortcode( $post->post_content ) );

do_shortcode triggers all visual composer shortcodes and thus returns html+text;

strip_tags removes all html tags and returns plain text.

Solution 3

Best solution, solved.
Just add the following code to file wp-includes/rest-api.php, at the bottom:

/**
 * Modify REST API content for pages to force
 * shortcodes to render since Visual Composer does not
 * do this
 */
add_action( 'rest_api_init', function ()
{
   register_rest_field(
          'page',
          'content',
          array(
                 'get_callback'    => 'compasshb_do_shortcodes',
                 'update_callback' => null,
                 'schema'          => null,
          )
       );
});

function compasshb_do_shortcodes( $object, $field_name, $request )
{
   WPBMap::addAllMappedShortcodes(); // This does all the work

   global $post;
   $post = get_post ($object['id']);
   $output['rendered'] = apply_filters( 'the_content', $post->post_content );

   return $output;
}
Share:
14,889

Related videos on Youtube

Harish Kumar
Author by

Harish Kumar

|- Back end developer with more than 10 years of experience. |- Created back-ends using Java (Spring Boot), PHP, Python (Django) |- Also do HTML, CSS, Javascript for FUN! :D |- Primarily worked on ecommerce and accounting integrations domain. |- Created integration application for accounting products like sage, quickbooks, xero, myob etc

Updated on June 04, 2022

Comments

  • Harish Kumar
    Harish Kumar almost 2 years

    I am working on a web-service(API) where i am fetching result WP_query() function and parse that in JSON format. which will further use in android application. The problem is the post_content i am getting with query is composed by visual composer and the whole content is in form of such tags like

    [VC_ROW][/VC_ROW][VC_COLUMN]some text[/VC_COLUMN] etc.
    

    I want to remove/strip all these shortcode from the content and retrieve only plain text from it. Is there any visual composer function through which i can achieve this thing

    <?php
    require('../../../wp-load.php');
    require_once(ABSPATH . 'wp-includes/functions.php');
    require_once(ABSPATH . 'wp-includes/shortcodes.php');
    header('Content-Type: application/json');
    
    $post_name = $_REQUEST['page'];
    
    if($post_name!=''){
        if($post_name=='services') {
    
        $args = array(
            'post_parent' => $page['services']['id'],
            'post_type'   => 'page', 
            'post_status' => 'published' 
        ); 
        $posts = get_children($args);
        foreach($posts as $po){
            $services_array[] = array('id'=>$po->ID,'title'=>$po->post_title,'image'=>get_post_meta($po->ID, 'webservice_page_image',true),'description'=>preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $po->post_content));
        }
    
        $post = array(
            'status'=>'ok', 
            'services'=>$services_array
        );
        echo json_encode($post);
    }
    }
    ?>
    
    • Rahul K
      Rahul K over 7 years
      Have you tried ? regex preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $the_content);
  • Harish Kumar
    Harish Kumar over 7 years
    for me now this code works str_replace('[vc_column_inner width="1/3"]','',preg_replace("~(?:[/?)[^/]]+/?]‌​~s", '', $po->post_content)) there are actually different parameters in vc_column, so if they comes i will remove them with str_replace. your answer make a little help. i will upvote it but its not the accurate solution
  • Rahul K
    Rahul K over 7 years
    Look I have updated code also parameter not matters in shortcode, this regex only checks shortcode that you have used in array. you can run this code still VC_COLUMN_INNTER passed in array then and then it accepted in content with parameters.
  • Zaheer Ahmad
    Zaheer Ahmad about 5 years
    It's working but issue is when we update WordPress then issue occurred, can't we add this or create custom plugin for this?
  • wbq
    wbq over 4 years
    sorry, but neither the first nor the second preg_replace line removed the shortcodes from my content string.
  • wbq
    wbq over 3 years
    Altering core files is the badest thing you could do.
  • wbq
    wbq over 3 years
    after some research this was the smartest way of dealing with the issue.