0
votes

im updating my WordPress website and removing the https://wpbakery.com/ editor and I'm wanting to remove the markup generated by the plugin.

This plugin https://codecanyon.net/item/shortcode-cleaner-clean-wordpress-content-from-broken-shortcodes/21253243 I'm using will remove all shortcodes with no problem except one. It's removing images.

On closer inspection images are posted on the backend using the below shortcode

[vc_single_image image="10879" img_size="large" add_caption="yes" alignment="center"]

And I want to update all references to use HTML instead

<img src="IMGURL">

However, I'm not sure about the right way to do it, any advice, please?

1
Do you want to actually replace all shortcode tags in the wordpress post edit content itself, or only front-end? If you want to also have them replaced in the backend, I think you will need to use PHP to update all posts, replace them with the image source by ID, and save the post. If you are oke with only having them display the image source frontend, you could register the shortcode youself (if you dont have WPBakery installed anymore) and use the param image="XXX" to get the image source, and echo the img tag with the source. - Jarvic

1 Answers

2
votes

There is a regular expression replace in MySQL 8, but if you don't have that, you could do it with PHP.

$query = new WP_Query( [-- whatever posts you want--] );
while ( $query->have_posts() ) {
    $query->the_post();

    $content = get_the_content();

    preg_match('/\[vc_single_image image="(\d+)" img_size="(\w+)"[^\]]*\]/', $content, $matches);

    if( isset($matches[1]) ) {
       $url = wp_get_attachment_image_url( (int) $matches[1], $matches[2] );
       $img = sprintf( '<img src="%s" />', $url );

       $new_content = str_replace( $matches[0], $img, $content );

       wp_update_post( [ 'ID' => get_the_ID(), 'post_content' => $new_content] );
   }
}