Don’t Fear The Shortcode
WordPress shortcodes are markers that allow an author to inject enhanced content at a specific point in the post. For example you might want to include a quote or a random photo into your blog post, for which it might look like [quote]your great quote goes here[/quote] or [photo]. Some think of shortcodes as code instead of content. So why not have the author just write HTML? In truth, shortcodes are an interface. The similarity of a shortcode and an HTML tag is superficial.
Short codes require a PHP function to be written, but it’s usually a developer who writes the code. What usually happens is that the code is added to the theme. When they change the theme for their website, the code for the shortcode doesn’t run anymore. This scenario happens enough to where people blame the shortcodes, and people become afraid their website will look broken.
It’s understandable that people don’t want their site to appear broken, but
there’s a better solution – one that will still allow you to use shortcodes.
The problem described above occurs because the PHP code that generates the output for the
shortcode was put in the theme – usually functions.php. The solution is to move that code from the theme into a plugin – one that is specific to your site and is always enabled. Using a plugin will allow you to change your theme at will without worrying about losing functionality.
What if you have short codes already in your content, but don’t have the original theme
code that rendered it? The answer is to write your own function to attach to the shortcode. I’ve provided two example that remove the shortcodes without severe consequence
add_shortcode('example', 'empty_content'); add_shortcode('quote', 'passthru_content'); /** * expect nothing, return an empty string * * @return string */ function empty_content(){ return ''; } /** * Use the content provided by the user * * @param array $attrs * @param string $content * @return string */ function passthru_content($attrs, $content){ return $content; }
Shortcodes can provide a variety a html. Expecting an author to create potentially complex HTML across multiple posts isn’t reasonable – especially for non-technical authors. So the next time you want to use a shortcode, go ahead – you can always change the function later to alter the output. Just remember to put it in a plugin