In WordPress, there are a few plugins you use that will require you to put a shortcode into your post, like [some-random-shortcode]. One disadvantage of using a plugin or theme that relies on shortcodes is that when you switch a theme or deactivate the plugin, they will leave behind shortcode tags in your posts which will look strange to your readers. In this article, MangoWP will show how to find and remove unused shortcodes from your WordPress posts and pages.

If you have an inactive shortcode on your site, then it will look like this in the content:

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

1. Remove the shortcode from database

MangoWP do not recommend beginner users to run SQL queries directly on their WordPress database.

In case, you know which specific type of shortcode you want to delete and you want to delete it permanently from your database, you just need to execute an 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 values, making it hard to form a catch-all SQL query.

Don’t forget to backup the database before running the above query. After deleting all shortcodes, optimize the WordPress database.

2. Remove all unused shortcodes from content (Recommended)

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

So 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.

3. Replace the shortcode with empty value

The last way, 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 ‘shortcodename’  with the actual shortcode.

add_shortcode( 'shortcodename', '__return_false' );

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.

We hope this article helped you find and remove unused shortcodes from your WordPress posts or pages. Please leave a comment below if you have any questions.

Leave A Comment