How to remove broken and unused shortcodes
When using shortcodes on your WordPress website, you can find that it makes adding redundant code across your website a whole lot easier as well as makes any changes to the shortcode apply to all instances of that shortcode all across your website. However, you may find that once you stop using a short code, there will be remnants of its existence all across your website leaving broken shortcode text in its place.
To fix that, in this article I will offer you three solutions and show you how to remove broken and unused shortcodes.
Manually Remove Shortcodes
Method number one is the most obvious and tedious way to remove them, and that is going through the entire website and removing it from the places you placed the shortcode. This method is best used only when you have a couple of instances of the shortcode website all across your website.
But however, you will find that removing these when you have way more than 10 instances of these shortcodes, can be way too much to remove so there has to be a faster solution, and lucky for you there is.
Hide Shortcodes from Users
An easy way to remove these shortcodes is to hide them by using the following code and pasting it in your functions.php file. This piece of code replaces the shortcode with an empty value:
-Place the shortcode name in as the first argument
Hide Shortcodes from with Regex
You also have the ability to hide all unused/broken shortcodes all across your website using this piece of code that uses regex to hide unused shortcodes all across your website. However, there are limitations with this method as they will not remove all shortcodes.
Paste this code into you funtions.php file:
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;
}
Use a Plugin to remove shortcodes
You can use the plugin called “Hide Unwanted Shortcodes” to remove them which has an easy-to-use interface to do that.
All you have to do is:
- Search “Hide Unwanted Shortcodes” Plugin install and active
- Go to tool > Hide Unwanted Shortcodes > Follow instructions listed on the page