Many theme developers provide shortcodes in their themes for various functions, such as inserting elements into posts. Similarly, many plugins also use shortcodes for their functionality, such as adding ad codes to posts. Using shortcodes is a convenient way to add elements to posts, but the main problem arises when we switch themes or disable plugins. They can leave shortcode tags behind in the posts, which may look unprofessional and unattractive 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 from the post completely.

remove-shortcode

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.

Pin It on Pinterest

Shares
Share This