How to Remove Unused Shortcodes From WordPress Posts

Many theme developers provide shortcode to their theme for some functions like insert element to the post. Besides this many plugins also use the shortcode for their function such as insert ad code to the post. Using of shortcode is comfort way to add elements to the post, But The main problem arise when we switch the theme or disable the plugin, They will leave behind shortcode tags in the posts which will look ugly to your readers.

If you have shortcode to one or two posts only, you can delete manually but if you have hundreds of posts, this is very long process. In this article, We will show you how to delete unused shortcode remove from the post completely.


How to remove completely Shortcode tag from posts

You can delete desired shortcode from all of the posts by running following query on your WordPress database.

UPDATE wp_post SET post_content = replace(post_content, '[shortcode]', '' ) ;

Replace the [shortcode] with the real shortcode which you want to remove. Don’t forget to backup the database before running the above query. After deleting all shortcodes, optimize the WordPress database.

If you don’t want to remove shortcode, you can hide shortcode to display your readers by adding following code to function.php file or functionality plugin. Replace ‘shortcodetag’  with the actual shortcode.

add_shortcode( 'shortcodetag', '__return_false' );

If you use many shortcodes in past and now don’t know which shortcode you use on which post. In that situation, you can hide all of the unused shortcodes by adding following line to your active theme function.php file.

add_filter('the_content', 'zole_remove_unused_shortcode');
function zole_remove_unused_shortcode($content)
{ $pattern = zole_get_unused_shortcode_regex();
$content = preg_replace_callback( '/'. $pattern .'/s', 'strip_shortcode_tag', $content );
return $content;

function zole_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;

This code retrieves the all active shortcodes and explore the content for shortcodes and remove the shortcodes which are not active shortcode. This code disables the unused shortcodes by removing content between shortcode tag. When you reactivate the plugin again the shortcode will work again.