When going through various themes and plugins, you might use various shortcodes on your site or on a client site. When making the transition from one theme to another or from one plugin to another something might happen. You could forget a page or post where you have used a shortcode. Such unused shortcode could break your site. Let’s see how to find those pages and posts.

How to find Unusued Shortcode

You can go to each post, page or any other custom post type and see which shortcode was not parsed into a styled text, form or something else. We are not doing that!

But we will do that through our code. We will have to parse all the content so that every possible shortcode that is registered gets parsed. With that approach, we will be left only with the shortcodes that once were registered (by a deactivated plugin or theme).

Coding our Search Engine

Our seach engine for unused shortcodes will be done on the admin side. You can add this code as a separate plugin or in your own existing plugin or theme. That is up to you. Let’s begin:

We are using the hook admin_init so that this code will be able to get processed only on the admin side. As you can see by the code, if you add ?get_shortcodes on your yoursite.com/wp-admin URL, you will start our search engine. We are also using die() to stop any further processing of our site.

Building the Query

We will use WP_Query to get us all our posts and pages. You can also add other custom post types in that search.

In the query arguments, we are putting the post types we are searching. I have also set that we want all the post types and that we don’t care about the status of them (since we have placed the value any in the post_status parameter).

Before making the query, we are setting the time limit to 0 in case we have a large database. This could be done in steps by adding another querystring &step=STEP and then using the parameter paged or offset inside the query arguments to paginate it. I leave that to you as a challenge!

We are also creating an empty $posts array. That array will hold all the posts with possible unused shortcodes. After that, we are looping through our custom loop.

Finding the Unused Shortcodes

This is probably the part you were waiting for. Put this code inside our custom loop.

So, what is happening here? First, we are buffering the output by ob_start(). What does that mean? That means that all the output is saved inside the memory instead of being shown on the site. With the function ob_get_clean() we are getting that back from the memory inside a variable.

Once we called the_content() function, WordPress has parsed all the registered shortcodes in that content. Now, we can only be left with the unused shortcodes.

The regex I am using here was copied from the do_shortcode WordPress function found in the file wp-includes/shortcodes.php. After that, if there is any match inside that post, we putting that inside our array. Now, it is time to show that.

Displaying Posts with Unused Shortcodes

This code will go after our custom loop and it is a basic example of working with an array.

For each post in our array, we are displaying a list item with the admin edit link (with admin_url) so that we can go and edit it. We are also displaying the possible unused shortcode. Since some of those matches might not be an unused shortcode, we are displaying the shortcode tag so that you can see if that is actually something to remove from the content.

Example of my older content:

Conclusion

Now, by visiting your admin dashboard and adding ?get_shortcodes, you will get a list of possible unused shortcodes and posts where you can find them. Now you just have to click on each edit link and remove it from there.

Remember the challenge? Try adding a step there so you can easily go through chunk of pages or posts.

What else can be done? You could save each post with unused shortcodes in a simple list. Show that list with a simple remove button. By clicking on that button, you could remove such shortcode from the content.

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

6 Comments

  1. Great idea, this is always a nightmare if you change themes because it’s easy to miss random shortcodes from the old theme in old blog posts – thanks!

    Reply

  2. Short and useful. Thank you!

    Reply

  3. Mohamed Abd ElHalim January 22, 2018 at 6:15 pm

    Thanks, Igor.

    What about this new plugin ( Shortcode Cleaner – Clean WordPress Content from Broken Shortcodes )
    https://plugins.jozoor.com/shortcode-cleaner/
    I think it will help more for that.

    Regards.

    Reply

    1. Hi Mohamed, sure, it might help. But this article is not about what to use in such scenario, it is about development of a tool that can help so other developers using WordPress can learn something new 🙂

      Reply

  4. Hi! Great article.
    You can also use plugins like Shortcodes Finder (https://wordpress.org/plugins/shortcodes-finder) to search a specific shortcode location or find unused shortcodes 😉

    Reply

  5. Antonio Gallo July 2, 2023 at 6:37 pm

    The regular expression seems broken and not working.

    Reply

Leave a reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.