How to remove specific shortcodes from post content.

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.

// 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.

Download it here.

Screen Shot 2015-05-26 at 11.48.50

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.

Always make a database backup before bulk replacing stuff in the database with a regular expression! Even if you’re use a plugin! Even if you’re 100% sure nothing bad will happen!

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.

Screen Shot 2015-05-25 at 15.49.09

These are the results for my [related_posts_by_tax] shortcode.

Screen Shot 2015-05-25 at 21.05.27

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.

6 thoughts on “How to remove specific shortcodes from post content.

  1. Hello, i use Shortcodes Ultimate in my site, but now i don’t use anymore and i want to delete all the old shortcodes because posts looks terrible. Can you help me about a plugin maybe wich can remove unused shortcodes?

  2. I’m trying to remove all the shortcodes from a theme so that I can use a different theme. It is challenging!! However, when I use your two plugins, and I enter one of the theme shortcodes, get the Regex and search, I get no results.
    For example, one shortcode begins with “[av_section min_height” and then there are many variables following that. If I enter just “av_section” it finds nothing. What am I doing wrong here, please?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s