Last active
June 11, 2023 01:47
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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