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:


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:

  function wpb_find_shortcode($atts, $content=null) {   ob_start();  extract( shortcode_atts( array(  		'find' => '',  	), $atts ) );    $string = $atts['find'];    $args = array(  	's' => $string,  	);    $the_query = new WP_Query( $args );    if ( $the_query->have_posts() ) {          echo '<ul>';  	while ( $the_query->have_posts() ) {  	$the_query->the_post(); ?>  	<li><a href="<?php  the_permalink() ?>"><?php the_title(); ?></a></li>  	<?php  	}          echo '</ul>';  } else {          echo "Sorry no posts found";   }    wp_reset_postdata();  return ob_get_clean();  }  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. This will allow you to see a list of all posts containing the shortcode tag you searched for.

How to Remove Unused Shortcodes in WordPress

Unfortunately, the best way to remove unused shortcodes from your posts is by manually editing each post containing the shortcode. In the method described above, we showed you how to get a list of posts containing a particular shortcode. Hopefully, this will save you some time. Once you have the list, then you can go through the posts one by one and remove the shortcode.

Alternatively if you don’t want to go edit your posts one by one, then there is a quick work around that would effectively hide the shortcode from appearing into your content. Simply paste the following code in a site-specific plugin or your theme’s functions.php file:

  add_shortcode( 'shortcodetag', '__return_false' );

You would need to replace shortcodetag with the shortcode appearing in your posts or the shortcode you want to hide.

Basically the code above will add the shortcode and make it show nothing. This way your shortcode will be parsed as any other registered shortcode would, but without showing anything in the output. If there are multiple unused shortcodes in your posts, then you can reuse this code by just replacing the shortcodetag with the shortcode you want to hide.

We hope this article helped you find and remove unused shortcodes from your WordPress posts or pages. For questions and feedback please leave a comment below.