Blog > WordPress Tutorials > How to Remove Unused Shortcodes from WordPress

How to Remove Unused Shortcodes from WordPress

Shortcodes—the great time saver for many WordPress users looking to build a site or add something without the hassle of coding from scratch. Now, I love to use Shortcodes as much as the next person, but they tend to be a remedy that can lead to plenty of other problems down the road.

For example, if you used these Shortcodes via a plugin or a WordPress theme and then stop using either one, then chances are that your site is now riddled with heaps upon heaps of shortcodes that no longer translate into anything but a mess—yay for you.

If you’ve been hit with this mess and want to save yourself the time of cleaning it all up on your own, then this post is for you!

How To Remove Unused Shortcodes from WordPress

How-to-Remove-Unused-Shortcodes-from-WordPress

So assuming that you have quite a few shortcodes that you no longer use, then, there is a seemingly simple fix to washing your site clean of all of them.

First, login to your WP dashboard and go to Appearance → Editor.

Remove-unused-shortcodes-step-1

Next, search through the links on the right side of your dashboard to find the functions.php file.

Remove-unused-shortcodes-step-2

Scroll to the bottom of your functions.php file and paste the following code just before the last ?> tag there:

add_filter('the_content', 'mte_remove_unused_shortcode');
function mte_remove_unused_shortcode($content)
{ $pattern = mte_get_unused_shortcode_regex();
$content = preg_replace_callback( '/'. $pattern .'/s', 'strip_shortcode_tag', $content );
return $content; 
}
 
function mte_get_unused_shortcode_regex() {
 global $shortcode_tags;
$tagnames = array_keys($shortcode_tags);
$tagregexp = join( '|', array_map('preg_quote', $tagnames) );
$regex = '\\[(\\[?)';
$regex .= "(?!$tagregexp)";
$regex .= '\\b([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)';
return $regex; 
}

When you save this code here, you will be telling WordPress to find and remove all of your shortcodes. Therefore, if you’re using shortcodes in your site still, this may not be your best option. Also, the code can, on occasion, do more harm than good.

Another option to cleaning up the look of your site is by hiding these shortcodes instead of messing around with the code or your site.

The easiest way to do this is to use the Hide Unwanted Shortcodes plugin.

Download and install the plugin and then navigate to the plugin’s setting options. There you will find a screen that asks you to place the shortcode tags you wish to hide. You don’t have to enter every single unused tag here (that would take as long as removing them all yourself). Instead, simply go after the single tags that you no longer use.

For example, say you have a whole bunch of tags that look like this:

[column-3] Here’s some random information [/column]

[column-3] Content from another page  [/column]

Instead of entering the whole string or sentence, you would simply enter [column-3] in the text box of the plugin’s setting area. Enter all the tags you wish to hide and then save it.

After that, your pages will look nice and sparkly clean.

Cleaning it up this way doesn’t actually get rid of the shortcodes, it only hides them, which is essentially like dusting that unsightly dirt in your living room under the carpet — not the best solution but at least your living room will look clean to everyone else.

Unless you know what you’re doing, it is generally best not to mess with the code of your functions.php file. So I suggest going with the second option of hiding the codes until you’re more comfortable with tweaking and adding code.

Comments (2)

  1. July 26, 2017
  2. August 8, 2017

Leave a reply

x