Posted on 19 November, 2009 By Kim Woodbridge 9 Comments

How to Create Your Own WordPress Shortcode

waiting for sunset (short-eared owl) (by ehoyer)

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.

(Anti) Social Development on Facebook

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


Related Posts:
  • How to add a RSS Feed to a Post or Page Using a WordPress Shortcode
  • How to Add a RSS Feed Without a Plugin Using the WordPress Fetch Feed Function
  • NextGEN Custom Fields Plugin: Add More Information about an Image in the NextGEN Gallery
  • How to Import Your WordPress Blogroll or Links From One Site to Another
  • How to Style the Read More Link in WordPress
  • Posted In : WordPress Tips
    If you enjoyed this post, please subscribe to my RSS Feed

    Post to Twitter   Post to Delicious   Post to StumbleUpon

    9 Responses to “How to Create Your Own WordPress Shortcode”

    Trackbacks/Pingbacks
    1. You are now listed on FAQPAL
    Leave a Comment
    You may use: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> .


    Recent Comments

      • softize.net: Hi Kim, I must say this guide was excellent for everyone. You give detailed instructions and security notices that ever...
      • Julie Walraven | Resume Services: This post stuck with me for a long time... I finally listened awhile back and dropped the connection. Then I realized in...
      • Kim Woodbridge: Hi Keith - I originally joined Facebook to find an old friend. I never expected to end up liking it as much as I do....
      • Kim Woodbridge: Hi Rajat - Thanks for the info! I'll try it out as soon as I get the chance....
      • Kim Woodbridge: Hi Timothy - I'm glad you agree - it seems that more people are using it. I'm not seeing quite as many FB updates that ...
      • Kim Woodbridge: Hi David - It is a fine line. My Mom used to sign me up for activities like gymnastics that I didn't want to do because...
      • Kim Woodbridge: Hi Ajith - This was her first time but I hope it gives her the confidence to continue. She did have a great time!...
      • David Rogers: Like you I was very shy as a child and would never have done anything like appear on stage. My children have recently le...