What WordPress shortcodes should be used for

I know that many developers cringe on the mention of shortcodes, especially ones included in a theme. Shortcodes are being abused to the extreme, replacing the good old HTML code with a supposedly “user-friendly” syntax. And then, if the plugin or theme that handles the shortcodes is uninstalled, you get ugly shortcode tags floating around in your content.

So when should shortcodes be used? My answer: never for static content. For example, if a shortcode named [notice] resulted in a <div id="notice"> which is then styled by the theme, then you’re doing it wrong. An example for a good use of shortcodes might be to output the current year. If we were making a copyright notice like:

Copyright 2013 ACME, inc

… we would need to go and update this notice every year. To resolve this, we can create a shortcode that uses PHP’s date() function to output the current year:

Copyright [current_year] ACME, inc

This is an example of what shortcodes really should be used for: bringing dynamic content to static posts. Shortcodes shouldn’t be used by themes (except in rare cases), and definitely shouldn’t replace HTML or CSS classes.

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.