I’ve used WordPress shortcodes before (in fact I’m using a few on this site) but I never really understood how they worked, and more importantly, how to create them.
Usually you find them in plugins, like the Contact Form 7 plugin which I’m using on this page. Whenever I want to add a contact page to my site all I have to do is type in [ CONTACT – FORM ] and my fully functional contact form appears as if by magic.
Pretty cool huh?
How do shortcodes work?
Shortcodes started in WordPress 2.5 with the introduction of a special set of functions called Shortcode API. As the WordPress Codex explains “the API handles all the tricky parsing, eliminating the need for writing a custom regular expression for each shortcode.” So the API allows the shortcodes to perform whatever they’re supposed to perform, and it does this automatically without you even knowing it.
All you do is add some php code to your functions.php file and you’re ready to go!
Creating your very own shortcodes
The WordPress codex does get into details on how shortcodes work, it explains the attributes, the output, the limitations and the like, but like most WP codex lingo, it’s not all easy to understand … and it’s definitely not a good how-to tutorial.
No worries, here’s one that is — Mastering WordPress Shortcodes by Smashing Magazine is a great tutorial, especially for beginners.
- It gives a good a few good examples of how shortcodes work
- Walks you through creating a simple shortcode so you get the hand of it
- Then goes into creating more advanced shortcodes
The best part is that once you go through all of the above, you’ll get to create some really neat shortcodes for your blog like:
- A “Send to Twitter” shortcode
- A “Subscribe to RSS” shortcode
- A shortcode for easily inserting Google AdSense anywhere
- A shortcode for embedding and RSS reader
- A shortcode that lets you get your posts from the WP database
Go ahead and give them a try! I’m going to be testing a few of these myself, so I’ll let you know how they turn out for me.