Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
}
}
?>
like image 472
Harish Kumar Avatar asked Aug 04 '16 09:08

Harish Kumar


2 Answers

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.

like image 139
Sharpey Avatar answered Sep 22 '22 01:09

Sharpey


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.

like image 33
Rahul K Avatar answered Sep 20 '22 01:09

Rahul K