Batch Update WordPress Shortcodes with WP-CLI Search + Replace

Shortcodes are super useful in WordPress. They allow you to easily output a feature or function anywhere on your site. But what happens if you no longer need the shortcode or want to update it to another shortcode?

In this tutorial there is a simple and advanced regex version for batch updating your WordPress shortcodes with new ones or removing them entirely. This is all possible thanks to the power of search and replace techniques.

Simple Shortcode Search and Replace with WP-CLI

I have a table called with a shortcode [tablecode id=multisite-table /] and I need to replace them with [shortcodertest=2 /]

I also know that I only want to change any string occurrences that are in the post_content column of the wp_posts table so I can restrict the replacement to just that column with the --include-columns flag

wp search-replace '\[tablecode id=multisite-table \/\]' '[shortcodertest=2 /]' $(wp db prefix)posts --include-columns=post_content --dry-run

If I simply want to delete the shortcode I could do that with this command

wp search-replace '\[tablecode id=multisite-table \/\]' '' $(wp db prefix)posts --include-columns=post_content --dry-run

Advanced Shortcode Search and Replace with regex

The shortcode I want to change using regex (short for regular expression) where sitetable just represents any table name for my shortcode

[tablecode id=<sitetable> /]

Here is an example shortcode I want to update from TablePress

[tablecode id=sitetable /]

I want to add the responsiveness to all of my tables so I need them to look like below

[tablecode id=sitetable responsive="scroll" /]

I also want any other occurrences of the tablecode id value regardless of which table it is to be updated since I want all of my tables to be responsive. To do that we use the power of regular expressions – regex to the rescue!

To activate regex for WP-CLI’s search and replace we use the --regex parameter (you can also use the --regex-flag parameter that is set to 'i' here which means to ignore the case of the strings – upper and lower case letters will be treated as equal).

The () sets a capture group for any character in the English alphabet a-z that can be re-used in the replacement string as a variable \1

wp search-replace '\[tablecode id=([a-z]+) \/\]' '[tablecode id=\1 responsive="scroll" /]' --regex --regex-flags='i' $(wp db prefix)posts --include-columns=post_content --dry-run


| Table    | Column       | Replacements | Type |
| wp_posts | post_content | 365          | PHP  |

If it looks OK then try without the --dry-run flag

wp search-replace '\[tablecode id=([a-z]+) \/\]' '[tablecode id=\1 responsive="scroll" /]' --regex --regex-flags='i' $(wp db prefix)posts --include-columns=post_content

If I wanted to delete all of the table shortcodes I could use a similar technique but remove the capture group since we don’t need to use that as a variable in the replacement string.

wp search-replace '\[tablecode id=[a-z]+ \/\]' '' --regex --regex-flags='i' $(wp db prefix)posts --include-columns=post_content --dry-run


WP-CLI search-replace Command