Earlier this week I wrote about the gallery shortcode that is built into WordPress and is used to display a simple image gallery on a post or page.
Shortcodes are basically shortcuts for inserting something into your post or page that is used frequently.
I can’t believe that I didn’t pay attention to them earlier, excerpt for some plugins that use them, as they have been around since WordPress 2.5
You can create your own shortcode for any phrase that you use often. This does, however, require writing a function and editing the functions.php file.
For example, say at the end of all Facebook articles that I write I wanted to add a link to my Facebook fan page.
In my functions.php file I would add:
function facebook() { return' <a href="http://facebook.com/Anti.Social.Development" title="Facebook" rel="nofollow" target="_blank">(Anti) Social Development on Facebook</a>'; } add_shortcode('face', 'facebook'); ?>
I named the function and then it told it to return the link to Facebook that I specified. I then told it to add the shortcode, which includes two paramters – the name of the shortcode, face, and the name of the function.
The new shortcode is added to the post or page by simply typing:
[face]
This will add the following link in the post. [face]
If I used this link frequently, using the shortcode would be a lot faster than manually adding the link.
This is a simple example of a shortcode and how it can be used. For more advanced examples, please read the article, Mastering WordPress Shortcodes, by Smashing Magazine.
Can you think of something that you write or use frequently that you would like to write a WordPress shortcode for?
photo credit: ehoyer
Christopher Dell says
This is so simple yet so incredibly awesome! Thanks for the tip, I will certainly be using this!
.-= Christopher Dell´s last blog ..IE 6 Update =-.
Kim Woodbridge says
Hi Christopher – I know, right? It’s fairly easy and really useful.
Rick Castellini says
Did I say you were amazing yet? Hard hitting, practical tips that keep me moving forward with my WP knowledge…thanks!
.-= Rick Castellini´s last blog ..Computer Help Show Podcast – 11-18-09 =-.
Kim Woodbridge says
Thanks Rick – I’m glad you liked the shortcode information this week :-)
Dave says
That’s the thing with the functions.php file. Everyone is scared of it right up until the moment they realize just how useful it can be. Good tutorial!
.-= Dave´s last blog ..Add the WordPress search function to your Thesis header =-.
Kim Woodbridge says
Thanks Dave – I’ve messed up my functions file a number of times ;-) Like anything else – just make a copy of it before you start editing.
Dennis Edell says
Ack more coding!!! You’re killin me Kim; how much do you charge so I don’t screw up my blog. ;)
.-= Dennis Edell´s last blog ..IS An Advertising Page Really A Good Idea? An Interesting Opposing View… =-.
Kim Woodbridge says
Hi Dennis – Just make copy of the page you are editing before you change anything and then you could put it back the way it was if anything
gets messed up :-)
Dennis Edell says
Haha! Most would be ashamed to admit the following on a popular public blog, but here goes…..
I was always one of those super smart good little boys that used the backup plugin faithfully, just in case of disaster…
…well, one day as I was upgrading the blog, it did indeed crash. Woohoo I have the backups! (I may have even said that out loud).
When suddenly…..OK how the hell do I restore the backup to this damn thing, I mutter incoherently to myself as I reach for the vodka and pills.
Good thing my host was Johnny on the spot and for only a $15 fee. ;)
.-= Dennis Edell´s last blog ..IS An Advertising Page Really A Good Idea? An Interesting Opposing View… =-.
ARS says
Thanks kim! This is very useful for me :)
Kim Woodbridge says
Great! What are you going to use it for?