Skip to content

Instantly share code, notes, and snippets.

@phillcoxon
Last active March 11, 2021 11:31
Show Gist options
  • Save phillcoxon/1968b1b02a1737efa8b060a8d952773b to your computer and use it in GitHub Desktop.
Save phillcoxon/1968b1b02a1737efa8b060a8d952773b to your computer and use it in GitHub Desktop.
Strip Visual Composer shortcodes from posts
<?php
/*
Load selected posts, strip out Visual Composer shortcodes leaving the content intact and update the post again.
*/
require('wp-load.php');
require_once('wp-includes/functions.php');
require_once('wp-includes/shortcodes.php');
$args = array('numberposts' => -1,
'post_type' => 'post');
$posts = get_posts($args);
foreach($posts as $po){
echo "id: ".$po->ID." | Title: ".$po->post_title."<br><br>";
// Stackoverflow magic:
$po->post_content=preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $po->post_content);
// But first, let's test..
//$po->post_title = "foobar " . $po->ID;
if ( ! wp_is_post_revision( $po->ID ) ){
$post_id = wp_update_post( $po, true );
if (is_wp_error($post_id)) {
$errors = $post_id->get_error_messages();
foreach ($errors as $error) {
echo "error: " . $error . "<br><Br><hr><br>";
}
}
}
}
/*
// From http://stackoverflow.com/questions/38764170/how-to-strip-all-visual-composer-shortcode-tags-from-wordpresss-post-content-fe
$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;
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment