WordPress Shortcodes: 3 Essential Tips
Next week, I am going to feature 10 awesome shortcodes that you can use on your WordPress site. But before then, I wanted to give you a simple introduction to shortcodes, in addition to a few key tips that you should bear in mind when using them.
For those who don’t know, a shortcode is a simple reference you can place anywhere in your blog, that pulls a specific piece of code from a central location (i.e. the functions.php file) and places it in the shortcode’s location.
So, you could put together a piece of code that places social sharing buttons in a nicely formatted box, and then reference that code via a shortcode at any place in any part of your blog.
There are three main benefits of using shortcodes:
- You don’t have to copy and paste the same lines of code every time you want to perform a specific action.
- If you want to alter what is produced by a shortcode, you only have to change the code once (at source).
- You can achieve a lot of relatively simple tasks with a shortcode, rather than resorting to a plugin.
Now then, let’s take a look at those three shortcode tips I promised you.
1. Future Proof Your Shortcodes
Shortcodes can be unravelled by upgrades to your blog. It is something that Thord ranted about in this post. Fortunately, the solution to the problem is extremely simple – just place all of your shortcodes in a plugin. The code contained within the plugin will not be lost when you upgrade.
The idea of making a plugin might seem impossible to you, but it is in fact extremely simple – just check out this post from Joe. He is no coder, and neither am I – but we can both make basic plugins with ease.
Please note this method won’t prevent code from becoming deprecated and unusable – nothing (apart from regular updates) can prevent that. But the same can be said of code within any plugin, so the risk is equal across the board.
2. Use Shortcodes Outside of Posts/Pages
The most common usage of shortcodes is within your posts and pages. However, what if you want to use one elsewhere in your theme – say in the header? There are a multitude of reasons why you might want to do this. For instance, you could want to include a related posts widget at the bottom of every post. Rather than inserting the shortcode at the bottom of every single post, you could place it in your single.php file, ninja style.
Doing so is very simple – you just need to slightly change the manner in which you call the shortcode. This brief post from Sarah shows you how it’s done.
3. Easily Reference All Available Shortcodes
One potential stumbling block when it comes to using shortcodes is simply remembering all of them! However, there is a simple solution to that problem – download and install the Shortcode Reference plugin. This will place a handy little widget on your post/page screens that features a list of all available shortcodes.
The plugin is starting to get a little long in the tooth, but I have just tested it on a v3.3.1 WordPress blog and it performed perfectly.
That’s All Folks!
So there you have it – three tips that can help you make the most of shortcodes. The next logical step is to give you a whole heap of shortcodes that you can use on your own blog. And that is exactly what I am going to do tomorrow, so stay tuned!