With WordPress, some plugins or themes require you to use a shortcode in the post content to add extra functionality. This is not considered a good practice. When the time comes to deactivate such a plugin, its shortcode isn’t removed from the post content. It will still appear in the front end in its unprocessed form. Here’s an example of how that would look like:
There are many ways to hide unused shortcodes, but none of these methods remove the shortcode from the post content permanently. In other words, they don’t remove the shortcode from the post content in the database. They let it linger there forever.
This got me thinking on how I would tackle this problem. First off, I’ve created a plugin that lets you add shortcodes after post content. But what of all the old posts that still have shortcodes?
WordPress core uses a regular expression (regex) to find the shortcodes in the post content. What better way to find a specific shortcode in the post content as with the same regular expression WordPress core uses.
Retrieving the regular expression
WordPress uses the get_shortcode_regex() function to retrieve its regular expression. This regex finds all registered shortcodes in content. All we have to do to get the regex for a specific shortcode is make the function think there is only one shortcode registered. Our specific shortcode to be precise.
To show how this can be done, the following example prints the regex for the shortcode
[related_posts_by_tax] from one of my plugins. For an easier way to get the regex, change the regex produced by this example or use the plugin below.
<?php // The $shortcode_tags global variable contains all registered shortcodes. global $shortcode_tags; // Store the shortcode_tags global in a temporary variable. $temp_shortcode_tags = $shortcode_tags; // Add only one specific shortcode name to the $shortcode_tags global. // // Replace 'related_posts_by_tax' with the shortcode you want to get the regex for. // Don't include the brackets from a shortcode. $shortcode_tags = array( 'related_posts_by_tax' => '' ); // Create the regex for your shortcode. $regex = '/' . get_shortcode_regex() . '/s'; // Restore the $shortcode_tags global. $shortcode_tags = $temp_shortcode_tags; // Print the regex. echo $regex; ?>
The code produces this regex (WordPress 4.2.2 install).
As you can see it’s pretty easy to just change the shortcode name (
related_posts_by_tax) in the produced regular expression. But be aware WordPress might change it in future.
To make it easier for you I’ve created a little plugin to create the regex WordPress would use to find a specific shortcode. Put it in your plugins folder and activate. The settings page is at wp-admin > Settings > Shortcode Regex Finder.
Using a regular expression to remove a shortcode
I’m using the Search Regex plugin to replace shortcodes inside post content because it has one killer feature. It lets you preview what the regular expression finds before you do the replacing.
This all comes with a warning though. Here it is.
Okay, that should make it clear you need to make a database backup before proceeding 🙂
After making a backup, install the Search Regex plugin and go to wp-admin > Tools > Search Regex. Put the regular expression for a shortcode in the “Search pattern” field, check the “Regex” checkbox and click “Search”. Leave the “Replace pattern” field empty if you want to remove the shortcode.
These are the results for my
As you can see, it finds the shortcode in the post content of a post. For this example it only finds one result, but imagine if you inserted a shortcode in thousands of posts. The Search Regex plugin clearly shows what will be replaced in the excellent preview of the results.
If you are happy with the results, replace all instances of the shortcode by clicking “Replace & Save”.
Now it’s time to go over all your posts to see if the unused shortcode was successfully removed. If not, restore your posts with the backup you made.
Consider creating a review for the Search Regex plugin if this helped you out.
If you know of another way of doing the same task, or if you’ve used this approach, let me know in the comments.