How to Find and Remove Unused Shortcodes From WordPress Posts

WPBeginner» Blog» Tutorials» How to Find and Remove…

How to Find and Remove Unused Shortcodes From WordPress Posts

 

Last updated on by

 

 

Elegant Themes
How to Find and Remove Unused Shortcodes From WordPress Posts

 

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:

 

[some-random-shortcode]

 

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:

 

01 function wpb_find_shortcode($atts, $content=null) {
02 ob_start();
03 extract( shortcode_atts( array(
04         'find' => '',
05     ), $atts ) );
06
07 $string = $atts['find'];
08
09 $args = array(
10     's' => $string,
11     );
12
13 $the_query = new WP_Query( $args );
14
15 if ( $the_query->have_posts() ) {
16         echo '<ul>';
17     while ( $the_query->have_posts() ) {
18     $the_query->the_post(); ?>
19     <li><a href="<?php  the_permalink() ?>"><?php the_title(); ?></a></li>
20     <?php
21     }
22         echo '</ul>';
23 } else {
24         echo "Sorry no posts found";
25 }
26
27 wp_reset_postdata();
28 return ob_get_clean();
29 }
30 add_shortcode('shortcodefinder', 'wpb_find_shortcode');

 

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:

 

[shortcodefinder find='myshortcode']

Replace myshortcode with the shortcode tag you are looking for. Save your post or page as a draft and then preview it. Continue reading…

Leave a Reply