Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Find and Remove Unused Shortcodes From WordPress Posts

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
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:

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.


Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Simon Kelly says:

    What a tip! I’m moving a site from Canvas to Beaver Builder and this is just what I needed to find the Woothemes shortcodes without getting into the database. Easy. Thanks!

  2. Catherine Argyros says:

    Parse error: syntax error, unexpected ‘ob_start’ (T_STRING), expecting ‘{‘ in /home2/archtyp3/public_html/wp-content/plugins/archtypesandcircles/archtypesandcircles.com-plugin.php on line 8

    I put this in my a site-specific plugin but got the above error message. please help.

    • i think ive got ti says:

      i just went back in and removed all the spaces, and its WORKING! thanks again for your time and energy. I really am an absolute beginner, but with this great support Im slowly getting it… :)

  3. Nehat says:

    Hello, my website is and i used before the Shortcodes Ultimate plugin, but now i don’t use that plugin and i want to remove all shortcode from that plugin leave in posts. Can you tell me for any plugin twhich can find the unused shortcode and delete automaticaly?

  4. yuvaraj says:

    is anyways to find it on pages in-spite of posts

  5. Giochi MMO says:

    There is a way to delete a specific shortcode, maintaining the text inside?

    For example: in this case [dropcap]A[/dropcap] I would like to eliminate the shortcode maintaining the “A”, or any other letter inside.

    Thanks!

  6. emeraldwave says:

    It would be helpful if you could also add how to bulk replace the old shortcodes found to the new shortcodes used in the new theme, rather than just removing.

    I think most people who migrate themes will be looking to replace like me.

    Regex replace plugin is useful but does not replace shortcodes in custom post types. So we still need some kind of bulk find and replace shortcodes solution.

  7. Paul Oyler says:

    Does this method still work w/ WP 4.0.1? If the shortcode we wanted to find was pb_carousel group=”x” what would be the shortcodefinder to use?

  8. Bill says:

    If the plugin is reactivated, should the shortcodes become functional again?

  9. Karen says:

    Very helpful. But a simple Search Regex search finds the shortcode for me. That seems far less intrusive than editing the function file?

  10. Paul says:

    This seems like it will be a really good idea to place in the plugin deactivation action, and not allow the user to deactivate the plugin until the shortcodes have been removed or just warn the user of which posts still have these shortcodes.

  11. raymond says:

    Why not write a filter for the_content that runs a regex looking for all short codes in the post then checks their name against the array of shortcode hooks to see if it had any hooked functions and if not remove them from being displayed. Theyre still there but then readers don’t see them if you need to edit a lot of posts. You could put that same funcrionality in a plugin to list all posts with all unhooked shortcodes in one location so the user doesn’t have to know the exact shortcode name when needing to edit out unused shortcodes.

  12. omid says:

    sorry i forgot to mention that we can get list all available shortcodesby global $shortcode_tags variable.

  13. omid says:

    i think there might be a way.as you used the loop to find post with shortcode we can take the id of each post and with some help from wpdb class and php regex probably the goal is achivable.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.