Skip to content

Instantly share code, notes, and snippets.

@jb510
Created January 25, 2019 02:52
Show Gist options
  • Save jb510/9412f574e7b489ca3cd118071e8c247c to your computer and use it in GitHub Desktop.
Save jb510/9412f574e7b489ca3cd118071e8c247c to your computer and use it in GitHub Desktop.
Cleanup Visual Composer Vomit (strip vc shortcodes)
<?php
/**
* Strip Visual Composer Shortcode Vomit
*
* Main footer file for the theme.
*
* @package DevTools
* @author Jon Brown <[email protected]>
* @license https://www.gnu.org/licenses/gpl-2.0.txt GNU/GPLv2
* @link https://9seeds.com
* @since 1.0.0
*/
/**
* Load need WordPress scripts
*/
require 'wp-load.php';
require_once 'wp-includes/functions.php';
require_once 'wp-includes/shortcodes.php';
/**
* Load all posts and strip Visual Composer Codes
*/
$args = array(
'posts_per_page' => -1, // Yes you can make this -1 if you scream YOLO before running.
'offset' => 0,
'post_type' => 'page',
// Make it faster!
'no_found_rows' => false,
// Skip caching!
'cache_results' => false,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
);
$posts = new WP_Query( $args );
// var_dump( $posts );
// wp_die('Just Testing!);
if ( $posts->have_posts() ) :
while ( $posts->have_posts() ) :
$posts->the_post();
echo '<br>ID ' . $post->ID . ' | Title ' . $post->post_title;
// Props to http://stackoverflow.com/questions/38764170/how-to-strip-all-visual-composer-shortcode-tags-from-wordpresss-post-content-fe
$post->post_content = preg_replace( '~(?:\[/?)[^/\]]+/?\]~s', '', $post->post_content );
if ( ! wp_is_post_revision( $post->ID ) ) {
$post_id = wp_update_post( $post, true );
if ( is_wp_error( $post_id ) ) {
$errors = $post_id->get_error_messages();
foreach ( $errors as $error ) {
echo '<span style="color:red"> - error: ' . $error . '</span>';
}
}
}
endwhile;
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment