Skip to content

Instantly share code, notes, and snippets.

@gemmadlou
Last active April 20, 2024 14:56
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save gemmadlou/7a0189bfae6c2c7268de12f1de822b8a to your computer and use it in GitHub Desktop.
Save gemmadlou/7a0189bfae6c2c7268de12f1de822b8a to your computer and use it in GitHub Desktop.
Removing Visual Composer & Shortcodes From Wordpress Content

Adding @k1sul1's suggestion from the comments as it's more concise than what I had before:

I just wanted them all gone, so I ran this in the MySQL shell.

UPDATE wp_posts SET post_content = REGEXP_REPLACE(post_content, "\\[\/?vc(.*?)\]", "");

Note the double backslash. If you forget it, you'll replace all v's and c's with nothing, and the shortcodes will still be there. This works for other shortcode as well, just replace vc.

Take a backup beforehand.

@mohamdio
Copy link

Thanks for sharing this tutorial, I developed a free plugin named ( Shortcode Cleaner Lite ), to solve this exact problem.
https://wordpress.org/plugins/shortcode-cleaner-lite
Video demo: https://www.youtube.com/watch?v=dXVZ5bSUduc

It will Clean your WordPress content from any unused broken shortcodes, which it clean up unused, broken shortcodes from WordPress content automatically, so you can switch between themes and plugins without worrying and keep your content cleanly and fresh all the time, it is dealing with any theme (Divi, Avada..etc) shortcodes that are left when changing themes or plugins or page builders (Visual Composer, Elementor, Beaver…etc).

I hope it will be useful.

@kondorwithak
Copy link

Thanks @mohamdio!

Your plugin worked perfectly to clean out Visual Composer / WPBakery Page Builder code in literally a single click.

Amazing work! Saved me hours!

@kkri
Copy link

kkri commented Sep 6, 2018

Just found the wonderful Shortcode cleaner lite plugin! After having cleaned the shortcodes, how to clean the databbase from Visual Composer tables? Thank you

@mohamdio
Copy link

@Internapse Thank you!

@kkri To clean up old/broken shortcodes within backend/database you should get the PRO version here:
https://codecanyon.net/item/shortcode-cleaner-clean-wordpress-content-from-broken-shortcodes/21253243
which the lite version just remove old/broken shortcodes within the frontend site.

@smarteseiten
Copy link

smarteseiten commented Feb 21, 2019

@mohamdio Can your tool REPLACE the shortcode by actual HTML-Code, or just delete or code-snippets?

Say like

[vc_row][vc_column][vc_single_image image=“12345″ img_size=“full“][/vc_column][/vc_row]

become

<img src="imagesource" alt="imagealt" />

edit: missing [code]-Tags added

@k1sul1
Copy link

k1sul1 commented Jun 11, 2019

I just wanted them all gone, so I ran this in the MySQL shell.

UPDATE wp_posts SET post_content = REGEXP_REPLACE(post_content, "\\[\/?vc(.*?)\]", "");

Note the double backslash. If you forget it, you'll replace all v's and c's with nothing, and the shortcodes will still be there.
This works for other shortcode as well, just replace vc.

Take a backup beforehand.

@gerrgg
Copy link

gerrgg commented Nov 13, 2019

I wrote a super short post about this: http://gerrg.com/snippet-how-to-clean-up-shortcodes-vs/

You can also do with with WP-CLI: wp search-replace --regex --verbose "\\[\/?vc(.*?)\]" "" wp_posts

@gemmadlou
Copy link
Author

Thanks for sharing this tutorial, I developed a free plugin named ( Shortcode Cleaner Lite ), to solve this exact problem.
https://wordpress.org/plugins/shortcode-cleaner-lite
Video demo: https://www.youtube.com/watch?v=dXVZ5bSUduc

It will Clean your WordPress content from any unused broken shortcodes, which it clean up unused, broken shortcodes from WordPress content automatically, so you can switch between themes and plugins without worrying and keep your content cleanly and fresh all the time, it is dealing with any theme (Divi, Avada..etc) shortcodes that are left when changing themes or plugins or page builders (Visual Composer, Elementor, Beaver…etc).

I hope it will be useful.

That's amazing! That makes life so much easier.

@gemmadlou
Copy link
Author

I wrote a super short post about this: http://gerrg.com/snippet-how-to-clean-up-shortcodes-vs/

You can also do with with WP-CLI: wp search-replace --regex --verbose "\\[\/?vc(.*?)\]" "" wp_posts

That's really neat. I didn't realise this gist had comments. Thanks for the wp-cli snippet.

@eddieteixeira
Copy link

Thanks for sharing this tutorial, I developed a free plugin named ( Shortcode Cleaner Lite ), to solve this exact problem.
https://wordpress.org/plugins/shortcode-cleaner-lite
Video demo: https://www.youtube.com/watch?v=dXVZ5bSUduc

It will Clean your WordPress content from any unused broken shortcodes, which it clean up unused, broken shortcodes from WordPress content automatically, so you can switch between themes and plugins without worrying and keep your content cleanly and fresh all the time, it is dealing with any theme (Divi, Avada..etc) shortcodes that are left when changing themes or plugins or page builders (Visual Composer, Elementor, Beaver…etc).

I hope it will be useful.

THANKS MAN! you saved my live! hhaha

@yupthatguy
Copy link

yupthatguy commented Nov 5, 2020

@mohamdio your plugin (free & pro versions) don't work with 5.5.3 multisite,... would be great if you would provide the paid support or give a refund.

@gemmadlou I am newbie.... trying to convert from WPbakery to Gutenberg... Can you tell me how to actually use the ".md: file that I downloaded. Thx.

Quick Update... I installed the "Import-Markdown" plugin (network activated)....I tried to import the ".md" file that I downloaded here....

got the the following error: "Unable to create the new post. An error occured."..... :-/

UPDATE: Newbie Woes... I figured it out... this thread was helpful... thx :-)

@gemmadlou
Copy link
Author

@mohamdio your plugin (free & pro versions) don't work with 5.5.3 multisite,... would be great if you would provide the paid support or give a refund.

@gemmadlou I am newbie.... trying to convert from WPbakery to Gutenberg... Can you tell me how to actually use the ".md: file that I downloaded. Thx.

Quick Update... I installed the "Import-Markdown" plugin (network activated)....I tried to import the ".md" file that I downloaded here....

got the the following error: "Unable to create the new post. An error occured."..... :-/

UPDATE: Newbie Woes... I figured it out... this thread was helpful... thx :-)

I'm glad you figured it out. I haven't been working with WordPress at all lately so I don't think I'd be very helpful anyway. Hope you have a great day or night.

@gastongarcia
Copy link

Thanks for sharing this tutorial, I developed a free plugin named ( Shortcode Cleaner Lite ), to solve this exact problem.
https://wordpress.org/plugins/shortcode-cleaner-lite
Video demo: https://www.youtube.com/watch?v=dXVZ5bSUduc

I hope it will be useful.

Your plugin is amazing! thank you.

Copy link

ghost commented Jul 1, 2021

I just wanted them all gone, so I ran this in the MySQL shell.

UPDATE wp_posts SET post_content = REGEXP_REPLACE(post_content, "\\[\/?vc(.*?)\]", "");

Note the double backslash. If you forget it, you'll replace all v's and c's with nothing, and the shortcodes will still be there.
This works for other shortcode as well, just replace vc.

Take a backup beforehand.

This is the hidden gem in this page! Thank you!

@michaelbourne
Copy link

I just wanted them all gone, so I ran this in the MySQL shell.

UPDATE wp_posts SET post_content = REGEXP_REPLACE(post_content, "\\[\/?vc(.*?)\]", "");

Note the double backslash. If you forget it, you'll replace all v's and c's with nothing, and the shortcodes will still be there.
This works for other shortcode as well, just replace vc.

Take a backup beforehand.

Cheers for the best reply here. We don't need a paid plugin for this.

@Jany-M
Copy link

Jany-M commented Nov 8, 2021

@gemmadlou @k1sul1 help with MySQL 5.6? :( can't use REGEXP_REPLACE (available only in MySQL 8+)

@gemmadlou
Copy link
Author

Hi @Jany-M - my method before was more manual as I would dump the SQL and fix it while migrating the site:

https://gist.github.com/gemmadlou/7a0189bfae6c2c7268de12f1de822b8a/210503ff67d9c1d220a8d69acbfc3479eca4b64f

Maybe a mixture of pure REGEXP with REPLACE can help?

https://dev.mysql.com/doc/refman/5.6/en/regexp.html

@praful2111
Copy link

Here Is the updated answer to update all images in the post/page content.

    // Args for the WP_Query
    $args = array(
    	'post_type' => 'post',
    	'posts_per_page' => 10,
    	'post__in' => array(5824),
    	'orderby' => 'post__in' 
    );
    
    // Execute WP_Query with args
    $query = new WP_Query( $args );
    
    // Start the Loop
    while ( $query->have_posts() ) {
    	$query->the_post();
    	// Get Page/Post content 
    	$content = apply_filters('the_content', $content);
    
    	// Get the vc_single_image count from the post 
    	$found_keyword = substr_count( $content,"vc_single_image" );
    
    	// Start loop to replace all elements from the content 
    	for ($i=0; $i < $found_keyword; $i++) { 
    		// Get the position of vc_single_image shortcode with Image ID and Image Size
    		preg_match( '/\[vc_single_image image="(\d+)" img_size="(\w+)"[^\]]*\]/', $content, $matches );
    
    		// Check shotcode are exist on loop
    		if( isset( $matches[1]) ){
    			
    			// Get the Image ur by Image id and Size
    			$url =	wp_get_attachment_image_url( (int) $matches[1], $matches[2] );
    			$img = sprintf( '<img src="%s" />', $url );
    
    			// Replce shortcode with <img> tag
    			$content = str_replace( $matches[0], $img, $content );
    
    		}
    	}
    
    	// Update post content with updated content with <img> tag
    	wp_update_post( [ 'ID' => get_the_ID(), 'post_content' => $content] );
    }
    // END the Loop

@jkriznar
Copy link

Hi, @praful2111

I'm having trouble with this code due to PHP Notice: Undefined variable: content in /var/www/example.com/htdocs/wp-content/themes/generatepress_child/functions.php on line 474 so the Woocommerce product content always gets replaced with blank content.

It's something with the line $content = apply_filters('the_content', $content);

I must add that I want to replace those image shortcodes on Woocommerce products, but I tested it with regular Wordpress post as-well and it doesn't work...

Can somebody please advise?

Thanks a ton.

@m4c
Copy link

m4c commented Apr 20, 2023

I still have an additional follow-up question. How to construct a regex query to MySQL to replace The7 VC gallery shortcodes to classic WP gallery:

From this:

[dt_gallery_masonry mode="grid" bwb_columns="desktop:2|h_tablet:2|v_tablet:2|phone:2" gap_between_posts="10px" image_border_radius="12px" image_scale_animation_on_hover="disabled" image_hover_bg_color="disabled" show_zoom="n" include="6205,6206,6207,6208,6209,6210" css_dt_gallery=".vc_custom_1664369671640{margin-bottom: 30px !important;}"]

to this:

[gallery size="medium" columns="2" ids="6205,6206,6207,6208,6209,6210"]

The idea is to keep and move the value of the 'include' parameter from dt_gallery_masonry to the 'ids' parameter in gallery.

I'll try this:

UPDATE wp_sandbx_posts SET post_content = REGEXP_REPLACE(post_content, '(\[\/?dt_gallery_masonry)(.*?)(include\=\")(.*?)(\")(.*?)(\])', '[gallery size=\"medium\" columns=\"2\" ids=\"$4\"]');

But something is wrong.

@nodsaibot
Copy link

mysql query is enough, NEVER pay for software, much less for plugins

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment