Posted on 19 November, 2009 By Kim Woodbridge 9 Comments

How to Create Your Own WordPress Shortcode

 
Share

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. You can also connect with my Facebook page or follow me on Twitter.



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

    CommentLuv Enabled

    Recent Comments

      • Ari Herzog: Agreed on the updates/inbox snafu. I rarely see page updates and I know where they are! .-= Ari Herzog´s last blog .....
      • jayson: Kim, Love the themes. I am looking to update the theme on my site to something more appropriate. My site is all abou...
      • vered: "I wish more people would use this. I think it’s silly when I get updates for things occuring in Kansas and California –...
      • Dennis Edell @ Direct Sales Marketing: I was speaking generally, not specifically this month. how exactly do you work things? .-= Dennis Edell @ Direct Sales M...
      • Lucy Beer: Unfortunately I think next to no-one actually sees those "Updates". They're buried in a subsection of the inbox and I do...
      • Kim Woodbridge: Hi Marbella - Thanks! I had a great time....
      • Kim Woodbridge: Hi Julie - I'll probably try kayaking again next summer but I don't know if I will become much of a kayaker - I had a di...
      • Kim Woodbridge: Hi Vered - If you ever get the chance, you should go. It's like a different world - it's so quiet and peaceful....

    About

    Kim Woodbridge is an accomplished Information and Technical Consultant specializing in the entire implementation of a WordPress based website including installation, theme design, upgrades, unique customizations and ongoing site maintenance.

    Wordpress Services

    • Installation, upgrades and maintenance
    • Conversion of existing html and css templates
    • Theme and plugin recommendations
    • CSS customizations
    • Troubleshooting and tweaks for unique situations
    • Customization for individual blogging goals and needs
    • Training and advice