How to remove broken and unused Shortcodes


To remove any broken and unused shortcodes, paste the following code into your function.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; 
}

Did you find this article useful?