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

      • Kim Woodbridge: HI Dennis - That's exactly right - it's like a macro for WordPress. Don't the Office programs still use them?...
      • Dennis Edell: Anyone needing to ask what a macro is, please do it privately...I hate looking so old in public. LOL! .-= Dennis Edell´s...
      • Kim Woodbridge: Hi Juncai - There isn't any mention of the alt tag in the fbml documentation for the share tag. I tested it by adding i...
      • Dennis Edell: The commonly used link is awesome. Kinda like a modern day macro, yes? .-= Dennis Edell´s last blog .....
      • Kim Woodbridge: Hi Shirley - I would be happy to answer your questions but I don't understand them. Are you referring to the Facebook s...
      • Kim Woodbridge: Hi Dennis - I could never park a car like that - even lining it up with the door in order to get into the house :-)...
      • Kim Woodbridge: Hi Carla - I didn't mean to sound snippy - I am a little irresponsible about it. I could get ok insurance if I tried h...
      • Kim Woodbridge: Hi Jamie - Thank you so much! And I'm really glad this helped you get the video on your fan page :-)...