How to Remove Unused Shortcode From Posts [WordPress]

In WordPress, if you have used plenty of plugins, you will surely come across a few that require you to put a shortcode in the post, like [shortcode_1], for it to work. Using shortcode may be a good way to add functionality to your site, but when you deactivate the plugin, the shortcode in your content don’t get removed and it will appear in the frontend in its unprocessed form.

This is how it looks like in its unprocessed form:

shortcode-not-rendered

There are several ways to remove unused shortcode from your site:

If you know which particular shortcode you want to remove and you want to remove it permanently from your database, you can simply do a SQL query with the command below:

UPDATE wp_post SET post_content = REPLACE(post_content, '[shortcodename]', '' ) ;

Replace “shortcodename” with the shortcode you want to remove.

Note: This is not a foolproof method because different shortcodes can come with different attributes and value, making it hard to form a catch-all SQL query.

If you know which shortcode to remove, but you don’t wish to remove it permanently, you can get it to render an empty value instead. Paste the following code at the end of the functions.php file (before the ?> tag) in your theme folder.

add_shortcode('shorcodename', 'mte_return_empty_shortcode');
function mte_return_empty_shortcode(){
	return '';
}

Replace the “shorcodename” with the shortcode that you want to remove.

Note: This is a good method only if you know which shortcode you are going to remove. You also have to make sure that the shortcode is no longer active, else it might results in conflict.

If you have a bunch of shortcodes that you no longer used and you can’t remember all the shortcode name, here is an ultimate way to disable all unused shortcode in your content.

Paste the code to the end of your functions.php just before the ?> tag.

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; 
}

What the above code does is to first retrieve the list of all active shortcodes, search the content for shortcodes that don’t match the active shortcode list and remove the shortcodes. It is modified from the get_shortcode_regex function that check for active shortcode.

Note: This method should work for all unused shortcode and it doesn’t remove the shortcode permanently. Next time you re-activate the plugin, its respective shortcode will continue to work automatically. On the flip side, this code will remove all content that are quoted between a “[" and "]” tag.