Post Pic

How to: Save time by using WordPress shortcodes

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

Dec 12 2008 10:21

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.

Dec 12 2008 10:26

Yeah, you can really do a lot of useful stuff with shortcodes!

Dec 12 2008 11:22

wow great..never heard that before..
will try it..many thanks!

Dec 12 2008 11:25

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..

Dec 12 2008 13:24

Glad you enjoyed this tip, hope it will be useful to you! :)

Dec 12 2008 13:43

The mind boggles as to the possible uses of something like this. Thanks a lot.

Dec 12 2008 14:43

@Lyndi: You’re welcome. Glad to see you enjoyed today’s recipe!

Dec 12 2008 15:39

isn’t there missing a ‘ after wprecipes in add_shortcode function? add_shortcode(‘wpr’, ‘wprecipes’)?

Dec 12 2008 15:47

@Bojan Pejic: You’re right, I forgot a ‘!!! Thanks for letting me know, I ust corrected it.

Dec 12 2008 15:56

@JBJ
No problem!

I liked the post :) Keep going with good articles.

Dec 12 2008 16:27

Thanks for your compliments Bojan!

Dec 12 2008 22:06

That could save a lot of time an effort. Very useful to know how to do that.

Dec 13 2008 04:42

Perhaps the most useful WordPress tip I have ever seen.. Thank you! :)

Dec 13 2008 08:03

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 :D

Dec 13 2008 09:30

@Jeff Starr: Thanks! As love your blog as well!

@Kevin Paquet: Thanks my friend!

Dec 19 2008 13:47

Nice tip – thanks for posting. Do the shortcodes support functions that accept parameters?

Dec 19 2008 13:51

@Shane: Yes it does. You can read the related recipe here.

Jan 01 2009 04:55

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

Jan 01 2009 11:21

@JK: You should read this article, I think it should help :)

Apr 21 2009 04:54

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. :)

Oct 22 2009 10:57

Nice one, would be implementing so., just implemented the load faster blog by compressing the wordpress hack of yours.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required