Remove Divi shortcodes

Divi to Gutenberg

Recently I had to create a WooCommerce with GeneratePress but taking advantage of all the previous products and pages, created with Divi.

In the end it is necessary to recreate one by one each of the pages, something could be done to map the Divi shortcodes and generate the necessary HTML, but it would still be a fix and what I really wanted was to keep “the “chicha”, the descriptions of the products (which were all laid out in Divi), the content of the pages and the posts, for which they had also used Divi.

Eliminating shortcodes one by one was practically unfeasible. The first option was a dump of the wp_posts table and then search and delete, but it gave some problems and I didn’t like the result too much.

Another option was to export the products to CSV, then search and replace, but due to the double quotes in the shortcodes, it was not a good idea either, although I did a good substitution of several thousand strings, I was not convinced.

The final solution I opted for, was that when saving a post, page or product, I would delete the shortcodes.

So I could edit a page, preview it with all the shortcodes and when I hit save, it deletes them all. But since it also works in quick edit, I can select 50 products, edit in bulk and hit save (without making any changes).

It may not seem to be the ideal method, but in this case it is the one that gave me the most control and I could select only the ones I wanted to “clean”. It would be very easy to apply a loop and execute it on all products, pages, etc…

I have only applied the “cleaning” of shortcodes in the content, I could apply it also in other fields, but in this case it was not necessary. However, have backups of the database before making any changes of this type.

To make this “fix” work, I used the WordPress filter that runs before saving an entry and that is content_save_pre that as we can see in https://developer.wordpress.org/reference/hooks/field_no_prefix_save_pre/ we can apply to the content (the one I used), title or any other field.

You can use this code in your function plugin or in the functions.php of your child theme, but remember to deactivate the filter (or delete the code) when you have already cleaned your posts from Divi shortcodes. Nothing would happen if it’s not disabled, but we’re not going to give our WordPress extra work every time it saves an entry if there’s no more code to clean up.

And here goes the code, which is what most will want and not all this previous literature, clarify that with very few modifications of the regular expression used, we can adapt it for other shortcodes.

/**
 * Clean Divi shortcodes from content Post.
 *
 * NOTE: Only for migrate from Divi to Gutenberg. Deactivate it after that.
 *
 * @param string $content The post content.
 *
 * @return string
 */ 
function cl_save_clean_content( $content ) { 
    return preg_replace( '/\[\/?et_pb.*?\]/', '', $content );
 }
 add_filter( 'content_save_pre', 'cl_save_clean_content', 10, 1 ); 

Added (20/03/2021): If we have removed Divi from our installation and we want to remove all traces of it, we must search in the options and post_meta table, those beginning with et_ and _et_, first we look for them:

SELECT * FROM wp_options WHERE option_name LIKE 'et\_%';
SELECT * FROM wp_postmeta WHERE meta_key LIKE 'et\_%';
SELECT * FROM wp_postmeta WHERE meta_key LIKE '\_et\_%';

And then, if we are sure that we are not going to use Divi again, we delete them (we can do it all in one query, but I prefer to go looking table by table and check with the SELECT the returned data):

DELETE FROM wp_options WHERE option_name LIKE 'et\_%';
DELETE FROM wp_postmeta WHERE meta_key LIKE 'et\_%';
DELETE FROM wp_postmeta WHERE meta_key LIKE '\_et\_%';

Remember that we must “escape” the character _ because in the SQL statement with the LIKE, it represents a character https://dev.mysql.com/doc/refman/5.7/en/string-comparison-functions.html.

Leave a Comment