
Shortcodes have been introduced in WordPress 2.5. They’re hooks which allow you to call a php function simply by typing something like [shortcode]. It is a great way to save time on repetitive tasks. Just read on to find out how to use them.

Shortcodes have been introduced in WordPress 2.5. They’re hooks which allow you to call a php function simply by typing something like [shortcode]. It is a great way to save time on repetitive tasks. Just read on to find out how to use them.
To create a shortcode, you first have to create a php function. Let's start with a basic one. Append it to your functions.php file.
function wprecipes() {
return 'Have you checked out <a href="http://www.wprecipes.com">WpRecipes</a> today?';
}
Once you created your function, you have to use the add_shortcode() function. paste this code just after your function on the functions.php file from your theme:
add_shortcode('wpr', 'wprecipes');
You're now able to use the wpr shortcode. To do so, paste the following line of code on the editor (in HTML mode) while writing a post:
[wpr]
This short code will output the "Have you checked out WpRecipes today?" message.
35 Responses
Interesting. I see other plugins like “TubePress” adopting this tech, and it does save me from creating too many page templates.
I mean, we could call functions through shortcodes in plain html environment.
Yeah, you can really do a lot of useful stuff with shortcodes!
wow great..never heard that before..
will try it..many thanks!
Just lovely..
Thank you so much.. I wish I knew something like that existed. You have made my life so much easy.
Thank you. Thank you. Thank you.
This is the best tip I have read so far..
Glad you enjoyed this tip, hope it will be useful to you!
The mind boggles as to the possible uses of something like this. Thanks a lot.
@Lyndi: You’re welcome. Glad to see you enjoyed today’s recipe!
isn’t there missing a ‘ after wprecipes in add_shortcode function? add_shortcode(‘wpr’, ‘wprecipes’)?
@Bojan Pejic: You’re right, I forgot a ‘!!! Thanks for letting me know, I ust corrected it.
@JBJ
No problem!
I liked the post
Keep going with good articles.
Thanks for your compliments Bojan!
That could save a lot of time an effort. Very useful to know how to do that.
Perhaps the most useful WordPress tip I have ever seen.. Thank you!
Very interesting and useful, if I only had the time to test all these, I would do so, but I’ve got the holiday season ahead of me to taste all these delicious recipes
@Jeff Starr: Thanks! As love your blog as well!
@Kevin Paquet: Thanks my friend!
Nice tip – thanks for posting. Do the shortcodes support functions that accept parameters?
@Shane: Yes it does. You can read the related recipe here.
Thanks for the great tip. This is what I have been looking for a while. What if the function takes 2 argument, how do you change the syntax in the []?
Would it be this if the two arguments to the function are strings?
[function 'First Argument', 'Second Argument']
Could you please let me know the correct syntax in this case?
Thanks again, JK
@JK: You should read this article, I think it should help
I use it to help my clients create code snippets. For example, when they create a page that requires a PDF download, I have the customary link to “Get Acrobat Reader” both in text and image format. I created that entire snippet of text and turned it into a shortcode: [get] and they use it with ease; makes site updates a breeze when the client is enthusiastic about adding content and things like this give them confidence.
Nice one, would be implementing so., just implemented the load faster blog by compressing the wordpress hack of yours.
Trackbacks: