Posted on 19 November, 2009 By 11 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:

Posted In : WordPress Tips

Did you enjoy this article? I can assist you with your WordPress or Facebook project. Contact me and I will provide you with an estimate. You can also connect with me on Facebook or follow me on Twitter.






11 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 badge

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.

Recent Comments

    • Marbella: Hi Kim, To get a Valentine card from a child who has been working with it for a few days means so much. They show their...
    • Gaynell M. Jones: Hi Kim, Wow, This is amazing! How can you read this much and blog at the same time?.. Well I'm not that a reader myse...
    • Gaynell M. Jones: Wow! This are cute! I will creat this for my darling. Thanks for the sharing this wonderful Idea....
    • Cmack: I found this website that shows how to have thumbnails link to another page by using the alt/title info in the gallery. ...
    • Lily Rose: This is so cute! I love it when people make their day and do the same for their close ones...

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