Shortcodes are WordPress functions that can be executed in a post or page by inserting a line of text wrapped in square brackets.
With a shortcode, you can insert and execute a php action, for example, list recent or related posts, add a feed, script or text. They simply bring functionalities of the sidebar widgets to post content – and much more.
Many plugins offer shortcodes to give users bigger flexibility in using them. In this post, I’ll try to convince you, that shortcodes are most useful if you create them by yourself.
Adding a shortcode is all about finding on the web a relevant piece of code, just like finding a WordPress plugin. Initial setup takes a couple of minutes, and then it’s just a matter of what you need.
What makes a difference is that you can always modify the shortcode, but it’s not easy (and not recommended) to modify the plugin.
Benefits
You don’t have to rely on a plugin
When you add a plugin, and use specific shortcodes associated with it, it’s all fine, except two situations:
- the plugin gets an update – the shortcode breaks, or the plugin breaks the blog,
- you remove the plugin – for instance, because it broke your blog, or because you found a better plugin.
What you are left with are strange pieces of text on your blog posts, like:
[this-shortcode number=”5″]
You can obviously remove them, one by one (time-consuming) or in bulk (risky), but you won’t bring back functionality served while the plugin was active.
You have full control over the shortcode
First thing is that you can modify the shortcode function any time to fit your current needs.
Another thing is that if you want to remove an element from a blog post, you can modify the relevant function to return no content. While the shortcode is still there, nothing is being displayed to a blog visitor.
And you can always use the code in the future, for a different purpose – as long as you use the same text between brackets.
You can fully customize the function
You’ll find examples in the Possibilities section, but here I’d like to point out that there are plugins that don’t offer shortcodes (and you wish they had) and there are plugins they offer shortcodes (but you don’t use them).
Finding relevant function on the web, figuring out how to implement it to your needs, takes more time than doing the same with plugins. However, when you learn the basics, you’ll see that if “there is no plugin for that”, there is always a way to find a function for that. It just needs some time and patience.
You have “a freedom to place”
The biggest benefit of shortcodes is that you can use them only when you need them.
Sometimes you don’t need to display a widget with recent posts on every single page of your blog. Sometimes you’d want to display recent posts from a certain category only on posts from that category.
On a higher level, shortcodes are the insert points. What will appear in the slot defined by square brackets is in your hands and can be modified anytime.
Possibilities
With shortcodes, you can insert in a post content a query, a script, a feed or a text. In most cases, what’s inside the shortcode goes also to the blog’s feed, it’s good to keep it in mind.
So, what you can do with shortcodes:
Add php query
You can execute any php query you want. Later today I’ll share a simple, but customizable related posts shortcode, so you’ll have a chance to see how it works.
You can add for example:
- posts from a certain category,
- posts with a certain tag or combination of tags,
- specific posts called by their ID,
- recent posts by certain author.
Basically, all you have to do is to find a relevant WordPress query and wrap it into a shortcode.
Add a feed
Adding a dynamic feed to post content is handled via shortcodes by a couple of plugins, but you can have your own one (and I’ll also share it, too).
Add a text
This kind of shortcodes I use most. You can insert any text, with links and styling.
It could be an invitation to subscribe to your blog or a message that your book is offered at a discount price till the end of the week. Possibilities are endless.
Add an image
When you insert an image in a post, turn the view from Visual to Text, grab the html code of the image and paste it inside the shortcode function – then you’ll have it displayed every time you call that shortcode.
Add a script
There are plugins that let add ad scripts into posts. I’ve been using ad manager plugin for a long time, also to add pieces of customized text and images, but when I learned how to add own shortcodes, I dropped the plugin completely.
The script of any kind can be used:
- AdSense or Amazon ads,
- sharing button or buttons,
- FeedBurner or MailChimp subscription form,
- embed code of a YouTube video or a SlideShare presentation,
- Facebook Like Box or Google+ Page Badge.
Anything that works when you insert it directly in a post will also work executed via the shortcode.
On the contrary, you can’t directly place php query in the post. What you can do is to place the shortcode that displays the outcome of that php query.
Example
Let’s assume you are sharing the development of your new novel which is about to be published in a couple of weeks.
You want to display an extra message under every post that covers the novel. But what you also want is the flexibility to change the message over time – in all new and old posts.
With a shortcode it’s possible, and you can do it by modifying the content of your shortcode like this:
function MyBookShortcode() { return 'Sign up to get a free chapter of my upcoming novel'; add_shortcode('my-book', 'MyBookShortcode');
Obviously, you can add links to the text. Also, by adding a div or p tag, you can style its class (here: “mybook”) to make it stand out from the post. You can add a custom background, border or make a font bigger.
To display a message in a post simply type [my-book] where you want it to be.
From now on, in all the posts with this shortcode the same message will be displayed – until you change it.
For instance, now you want to drive traffic to Amazon, to let users pre-order your book. Open your shortcode and change the text inside brackets that come after return.
From now on in all the posts that have [my-book] inserted, a new message is being displayed. Convenient isn’t it?
• • •
With own shortcodes, the first thing is to learn, but once you go through it, your blog will be customized the way it has never been before.
Below there is a list of posts about WordPress shortcodes (which, by the way is handled by a shortcode):
[pi-archive number=5 tag=”shortcodes” ]
32 Replies to “Why it’s good to use own shortcodes in WordPress”
Comments are closed.