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.