How to Hide Broken Shortcodes in WordPress Post Content

By Tejaswini Deshpande | April 7, 2011

Now-a-days almost all WordPress plugins and some of the advanced themes are offering ready to use and handy Shortcodes, that can be used in the post content to insert some formatted content. The content is either dynamically generated using the Shortcode attributes or  is provided by us when we use enclosing Shortcodes.

An example of simple shortcode could be

[ gallery id=”5″ ]

(Note: There is a space after the opening square bracket and before closing square bracket. The actual shortcode will not have this space)

The Need

[ad id=’9′ style=’float:left;margin:0 10px 10px 0′]Often we use such plugin for a certain time period, use the Shortcode provided by the plugin in the post content and after some days we get some better alternative rather than using this plugin and now we wish we could deactivate the plugin and stop using it. But then what will happen to all of the shortcode insertions in the post content specific to this plugin? If we deactivate this plugin, these shortcodes will become naked and visible to normal visitors of the site, which is not acceptable and would leave a bad impression on the readers. So before stopping to use the plugin, you will have to individually visit all the affected posts, remove the not to be used shortcode, implement the newly discovered alternative there and then deactivate the plugin. This could be an exhaustive procedure if you have a lot of such posts or if you have to search such posts from hundreds of the blog posts. So it would be a very good option to hide all the broken shortcodes in the post content rather than searching for them and deleting them.

For example, you have inserted YouTube videos in the posts using the [ youtube ] shortcode provided by a certain plugin. There are many such posts on your blog with such videos. Now if the plugin has broken for the latest version of WordPress or if there is a better plugin that you have found out or if the plugin is not upto standards, you might want stop using it. Once you deactivate it, all the [ youtube ] shortcodes will now appear as text in the post content, making the visitors to think what you wanted to put there.

The Solution

So, in order to hide the broken or naked shortcodes you can use a very lightweight plugin named ‘Hide Broken Shortcodes‘ by coffee2code which will not show all the broken shortcodes that rather than inserting formatted content, just appear as the text inside the post content. To use this plugin, just download the plugin zip file, unzip it, upload the plugin folder to the wp-content –> plugins folder of your WordPress installation and activate the plugin. The procedure being similar to regular plugin installation. There are no changes needed to any of the files or permissions and there is no settings page for the plugin.

[ad id=’15’ style=’margin:10px 50px’]

It is a very light and simple to use plugin. You just have to activate it and it starts working.

The algorithm for this plugin is really simple:-

  • After the working shortcodes in the content are replaced, the returned content is again scanned to find any strings resembling to a WordPress shortcode
  • If broken self enclosing shortcodes are found, they are replaced by a space.
  • If broken enclosing shortcodes are found, the content enclosed within the shortcode is shown, just the actual shortcode text is not shown.

Missing features

  • You cannot selectively hide broken Shortcodes. This plugin will not show any of the string that resembles to a WordPress Shortcode format
  • If the broken shortcode is within another broken shortcode, it will not hide the inside broken shortcode. The inside broken shortcode will be served as a naked shortcode text to the reader.

2 thoughts on “How to Hide Broken Shortcodes in WordPress Post Content

  1. Dustin @ Themespotter WordPress Themes

    Thanks for this post. Is there any way to hide a ‘valid’ short code? The shortcode still works, but I just don’t want to display it anymore and don’t want to remove it from every post on an individual basis.


Leave a Reply

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