Use $wpdb or other PHP script method to find/replace in WP database


I’m hoping to push a bug fix for a custom WordPress plugin my sites use.

The plugin registers a shortcode with various attributes.

However, there’s a bug where one of the attributes is incorrectly called, so it is never being rendered in the HTML.

Since I have a lot of users that use this plugin, it’s possible that they uknowingly have this attribute set in their shortcode on production pages—meaning pushing a fix could cause these pages to then render incorrectly.

I essentially want to run a script that "resets" all instances of the shortcode attribute to the default value ('', in this case).

I want to regex find all instances of:


and replace it with:

[shortcode_name $1 attribute_name=''

in wp_posts.post_content

I know older versions of MySQL (< 8.x) don’t have a native REGEXP_REPLACE() function, so I want something that is compatible between the myriad widely used versions of MySQL and MariaDB that are out there. I’m not really sure how to approach this with PHP and/or the $wpdb class (or if this is even a good idea, at all).

According to PHPCS, "PHP system calls are often disabled by server admins", so I don’t want to use shell_exec() to call a WP-CLI script.

user3183717 2 years 2020-12-28T14:10:28-05:00 0 Answers 3 views 0

Leave an answer