Tips and Tricks

WordPress Shortcodes Not Working? Make Shortcodes work in Theme,Plugin

Shortcodes in WordPress are a result of the Shortcode API, introduced in WordPress 2.5. Shortcodes are a simple set of functions for creating macro codes for use in content and can be used in Themes, Plugins, etc. At times, it’s possible that you may have used a shortcode in a Theme or a Plugin & expected it to work correctly but that didn’t happen. Or you might have noticed that the shortcodes stopped working after you made some changes to the existing code. If shortcodes stop working, then the functionality related to it is completely lost. So how to fix this? In this article, I am going to share some ways of fixing the WordPress shortcodes in a theme or plugin. Read to find out how.

WordPress Shortcodes Not Working? Try these fixes

Ok so let’s discuss this with a very practical example. Once I am finished with that, I will list out some other general ways that you can use to fix the shortcodes issue.

Just recently, I was working on a WordPress theme that required me to work with custom WordPress queries & change the content as per requirement. I was using get_the_content() to show the content. I had the WP Table Reloaded plugin running & I created a table with some data in it. (If you haven’t heard about it, then it is a very good plugin that lets you present data in tabular format. It’s the best plugin I have seen so far, when it comes to handling & presenting tables. You can check it out by visiting the official WordPress Website or by clicking here). I took the shortcode generated by the plugin, inserted it into a new post & saved the post. When I visited the website frontend, I noticed that the table is missing! How could it be? I checked everything again & could not find any possible issue. Upon further inspection, I found out that the shortcodes were completely ignored and they were never parsed. And then, I applied the following solution to make the shortcodes work.

Make shortcodes work for Post content for a custom WordPress Query

That’s it! I saved the code & when I refreshed the frontend, the tables showed up normally! So if you are absolutely sure that the shortcodes exist but they dont seem to work, then simply apply the function do_shortcode() on the resultant content to make the shortcodes work.

Ok, now that I discussed a specific practical example on how to make shortcodes work for content derived by using a custom WordPress query, I am going to list out some other general ways to make shortcodes work, should the above procedure not be applicable to your case:

Make shortcodes work on Post Content / Page Content for a standard WordPress Query

Make shortcodes work from within a Theme instead of with the content of a Post Content / Page Content and supply custom attributes for the shortcode

Example 1: Simplest form of Shortcode

Example 2: Custom values being passed to Shortcode – Method 1

Example 3: Custom values being passed to Shortcode – Method 2

Example 4: Using opening and closing shortcode

Make Shortcodes work and add custom field along with its value within shortcode

The code above may require some explanation, so here it goes. What we are essentially doing here is, we have a custom field in wp_postmeta table with meta_key ‘rating’. So in the first line, we are trying to fetch the value of this rating for the post_id as indicated. Now we want to pass this rating value to our shortcode so that the shortcode can grab, parse & read the rating value and use it for internal calculations & show output based on the same. So in the 2nd line, we are making sure that rating_value is not empty. And once we are positive that a rating exists, we are specifying the shortcode and passing the rating value by specifying the key ‘rating’ & assigning the ‘rating_value’ to it. Note that we are also passing some other values such as the post_id & the type.

IMPORTANT NOTES:

1. Make sure to single quote just before and after the square braces. Do NOT use double quotes or the solution(s) may not work.

2. Use echo in front of do_shortcode if you are using the code as standalone in your theme. If you are using do_shortcode on content from a post/page, then you may skip specifying echo.

I hope this article helped you more on understanding how shortcodes were designed to work and how you can troubleshoot and solve issues, should you find that any shortcodes are not working in WordPress theme or plugin.

Your Turn!

Do you know of any other ways to make shortcodes work in WordPress? Feel free to suggest by commenting below.

Share your thoughts, comment below now!

*

*