Skip to content

Instantly share code, notes, and snippets.

@thisbit
Last active June 11, 2023 01:47
Show Gist options
  • Save thisbit/8db4610821b0d81b2a740ae8aa44042c to your computer and use it in GitHub Desktop.
Save thisbit/8db4610821b0d81b2a740ae8aa44042c to your computer and use it in GitHub Desktop.
Remove WP Bakery / Enfold (Avia Builder) shortcodes from the post on clicking update/save
<?php
function remove_shortcodes_from_post_content($content) {
// Define the pattern to match the shortcodes
$pattern = '/\[(\/)?(vc_|av_)[^\]]*\]/';
// Remove the shortcodes from the content using preg_replace
$clean_content = preg_replace($pattern, '', $content);
return $clean_content;
}
// Hook into the wp_insert_post_data filter to clean the post content
add_filter('wp_insert_post_data', 'remove_shortcodes_on_post_save', 10, 2);
function remove_shortcodes_on_post_save($data, $postarr) {
// Check if it's a post update
if (isset($postarr['ID'])) {
// Retrieve the post object
$post = get_post($postarr['ID']);
// Remove the shortcodes from the post content
$clean_content = remove_shortcodes_from_post_content($post->post_content);
// Update the post content in the data array
$data['post_content'] = $clean_content;
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment