Shortcodes are great, but they are not always the best way. 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 shordcode tags in your posts which will look strange to your readers. In this article, we will show how to find and remove unused shortcodes from your WordPress posts and pages.
Are Shortcodes Bad?
No, absolutely not. Shortcodes are not bad, but overusing them can be problematic. For example, we use Compact Archives plugin which provides a shortcode and a template tag. We have the shortcode only on our archives page, so if we ever deactivate that plugin, then there is just one page we need to remove the shortcode from.
On the other hand, there are plugins and themes that provide shortcodes to create common style elements like buttons, tables, columns, etc. Some ad management plugins also use shortcodes. Now if a user has used these shortcodes in many posts, then it becomes very difficult for the user to remove the shortcode from all posts and pages.
This is why we recommend our users to not rely on themes or plugins which require you to add shortcodes into many posts. You should always try to find a better alternative if you can, or contact the theme or plugin author. They might tell you a better way to get the same functionality without using too many shortcodes in posts or pages.
For those still wondering, if you have an inactive shortcode on your site, then it will look like this in the middile of your content:
In order to remove unused shortcodes from your posts and pages, you need to first find them.
Find All Posts Containing a Particular Shortcode
We will try the simplest approach to find the shortcode inside post content. Simply copy and paste the following code in a site-specific plugin or your theme’s functions.php file:
In this code, we have created a shortcode (how ironic is that?). The shortcode runs a function to execute a custom WordPress Query. In this query, we are using the default WordPress search feature to find the shortcode and then list all posts found with that specific shortcode.
To use this, you need to create a new WordPress post or page and paste this shortcode inside it:
Replace myshortcode with the shortcode tag you are looking for. Save your post or page as a draft and then preview it. Continue reading…