Skip to content

Instantly share code, notes, and snippets.

@kbcarte
Last active March 21, 2022 20:01
Show Gist options
  • Save kbcarte/7cb03d19e1a23c472da4b5168a507787 to your computer and use it in GitHub Desktop.
Save kbcarte/7cb03d19e1a23c472da4b5168a507787 to your computer and use it in GitHub Desktop.
PHP Removes the old "[vc_]" shortcodes found in post_content during migrations. If other shortcodes not specific to vc_* are preset, the regex pattern will need to be updated.
<?php
// Add to functions.php
// Remove the "[vc_*]" shortcodes usually found during migrations
// Add this shortcode to a debug/test page and visit it
// The page will render and dump all new post/pages to the screen
// This can take a long time if there are a lot of pages/posts
add_shortcode('remove-vc-shortcodes', 'remove_vc_shortcodes');
function remove_vc_shortcodes() {
$pattern = '/\[\/?vc_.*\]?/i';
$blog_args = array(
'post_type' => 'post',
'post_status' => array('draft', 'publish'),
'posts_per_page' => -1
);
$blog_posts = get_posts($blog_args);
foreach($blog_posts as $i => $p){
$string = $p->post_content;
$new_s = preg_replace($pattern, '', $string);
$p->post_content = $new_s;
wp_update_post($p);
}
$page_args = array(
'post_type' => 'page',
'post_status' => array('draft', 'publish'),
'posts_per_page' => -1
);
$page_posts = get_posts($page_args);
foreach($page_posts as $i => $p){
$string = $p->post_content;
$new_s = preg_replace($pattern, '', $string);
$p->post_content = $new_s;
wp_update_post($p);
}
ob_start();
?>
<h1>Posts</h1>
<pre>
<?php var_dump($blog_posts); ?>
</pre>
<h1>Pages</h1>
<pre>
<?php var_dump($blog_posts); ?>
</pre>
<?php
return ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment