Using WordPress shortcodes to “memorize” links and save time

by Savvy Writers on March 5, 2010

All bloggers, but especially deal bloggers, tend to link to the same sites over and over again. Affiliates can be time-consuming to link to, because you have to look up the specific code for the specific offer. It’s a pain to have to look up affiliate codes on the affiliate website, or go to your own site to find a previous instance of the link to copy so you can make sure you get your affiliate code inserted properly.

I knew there had to be a better way, but all the plugins I found either didn’t work, were expensive, or worked too well and inserted links where I didn’t want them. Then, thanks to WPShout, I discovered shortcodes and I’ve never looked back. They save me so much time – I have shortcodes for affiliates, other blogs I link to often, my Facebook page, etc.

Shortcodes are really pretty easy to set up. Once you get one done, the rest are easy. And I guarantee you will feel geeky when you understand WordPress shortcodes!

To begin, we’ll need to edit our functions.php file (or custom_functions.php if you’re a Thesis theme user). Editing this file can break your site, so you will want to make a backup before you start editing. Here’s how to back up your site files. If there is a problem with your code, you can then simply upload your backup of functions.php and you’ll be fine.

Note that if you do break your functions.php file, you’ll need access to your file manager or FTP to upload the backup you hopefully saved – you won’t be able to access the WordPress admin panel while it’s broken. So, make sure you know how to get into the actual files of your site before editing.

So, first make a backup of your functions.php file. If you’re nervous, save one on your desktop, one in your Documents, one or two other places on your computer. (Just kidding – that’s not really necessary unless you are super-duper nervous… which you should be if you’ve never messed with this stuff before. ;)

Next, open your functions.php file, either in a text editor or in the WordPress theme editor area, and enter this code:

function yourfunctionname() {
    return '<a href="http://yourlink.com">YourLink.com</a>';
}
add_shortcode('TheNameYouWantToType', 'yourfunctionname');

You will, of course, want to change the names appropriately. The names highlighted in yellow are the function and shortcode names and must match each other. The first part of the code (the function) is telling the shortcode what exactly it’s supposed to do when inserted into the post. Do not put punctuation into the yellow part of the code – it won’t work. Also, know that yourfunctionname won’t show up anywhere except this .php file, so it can be called whatever. I usually call mine couponsdotcom if the shortcode is a link for Coupons.com.

The line highlighted in orange is where you need to put the HTML code for your link or image. Put it between the two apostrophes.

The line highlighted in pink is what you will actually be typing inside brackets to insert the code into your post. Make this something easy to remember, such as coupons.com. This is case sensitive, so be consistent when setting these up. I recommend doing it all lowercase so that you don’t have to remember what you capitalized and what you didn’t.

Once you’ve made all these changes, hit save and then go to your site to make sure it’s still working. :) If you get an error like “unexpected ‘/’ on line 22 of functions.php”, you’ve messed up and you’ll want to go load your backup functions.php file and start again.

I know it sounds confusing, but I guarantee it’s worth the effort – once you figure out how to do one, you’ll be saving so much time you won’t know what to do with yourself!

Now, to enter the shortcodes in your post, put the word highlighted in pink inside brackets, like this: [TheNameYouWantToType]. Then, in your post will appear YourLink.com, which is what you put inside the apostrophes in the orange area.

Here’s are three different functions that you could copy and paste into your functions.php file to see how it works.

function savvyblogging() {
return '<a href="http://savvyblogging.net">Savvy Blogging</a>';
}
add_shortcode('savvyblogging', 'savvyblogging');


function savvybloggingdotnet() {
return '<a href="http://savvyblogging.net">SavvyBlogging.net</a>';
}
add_shortcode('savvyblogging.net', 'savvybloggingdotnet');

function savvybloggingtwitter() {
return '<a href="http://twitter.com/savvyblogging">Savvy Blogging on Twitter</a>';
}
add_shortcode('twitter', 'savvybloggingtwitter');

If you’ve saved these in functions.php, they’re now active in your site. Here’s how you’d implement them in your post editor:

Remember that the shortcode has to be encapsulated in those brackets in order to show up.

And here’s the result in your actual post:

So, what are you waiting for? Stop wasting time hunting around for the same affiliate links over and over and make it easy on yourself by using shortcodes!

A few final notes: if you ever delete a shortcode in your functions.php file, any previous uses of that shortcode won’t work anymore, they’ll just show the “plain text” of the shortcode name (ie, [shortcodename]) since it’s no longer being told what to do.

Changing a link is easy – just edit the HTML part of the shortcode and all your instances of that shortcode will automatically be updated with the new link. I like this for affiliate programs that are available off and on. When there’s no affiliate link available for it, I can change it to just point directly to the home page of the destination site. Then, when an affiliate program becomes available again, I update it in fucntions.php and voila – all my links are updated, both old and new.

Your functions must be unique – they can’t share the same name or shortcode or it will likely break your site. Make sure you give them unique names.

And finally, you’ll probably want to come up with some way to keep track of the shortcodes you have so that you actually use them. I actually have my desktop background set as a cute picture of a puppy dog with my kids image of my shortcodes and website color scheme so I can access them quickly. After awhile, you remember what they are – I no longer refer to it for most codes, but as I add new shortcodes I add them to the desktop background and take others off so that I can remember what they are. A notebook works, too. ;)

If you’ve got questions, please feel free to leave a comment! Please note that I am not liable for any damage you may do by editing your functions.php file, nor am I able to help you fix it other than offering moral support. ;) Backup backup backup!

Carrie Isaac has broken her site many, many times and is grateful to now know enough that she doesn’t freak out when it happens! She blogs about deals in Colorado Springs at Springs Bargains and does internet marketing for her husband Jeremy, a Colorado Springs Realtor.

{ 1 trackback }

Recomendation: Auto link keywords - WordPress Tavern Forum
April 19, 2010 at 4:39 pm

{ 11 comments… read them below or add one }

Sara @ Happy Brown House March 5, 2010 at 8:14 am

So neat! I bet that is a huge timesaver! Is there anything like this for blogger?

[Reply]

Karen @ Saving The Family Money March 5, 2010 at 9:09 am

I can’t wait to try this! I currently have a “sticky” on my desktop with frequently used links! This will help me clean up :)

[Reply]

lynn @ Maven of Savin' March 5, 2010 at 9:23 am

Great info for when I move to wordpress. Any tips on doing it in blogger??

[Reply]

Savvy Writers Reply:

Not unless you are really, really, really, really, really, really geeky. :) I know it’s possible to edit the PHP of a Blogger blog but I’m not even sure if you could add shortcodes or how it works.

Let’s just say it would *probably* be easier to move your Blogger blog to WP one word at a time than try to add shortcodes to Blogger. ;)

[Reply]

Kristi @ Spend Less and Save More March 5, 2010 at 10:01 am

Can this be done for those of us in blogger?

[Reply]

Carrie Isaac Reply:

Kristi, not really. You should read Mandi’s comment below about using text expanders, as that might be a way to implement a similar sort of thing on Blogger.

[Reply]

Mandi @ Organizing Your Way March 5, 2010 at 1:17 pm

Ooh, cool! I never knew about these!

I do, however, use text expanders on my computer to accomplish the same thing. I use Shortkeys and have over 2000 text expanders that work anywhere on my computer (from notepad to Firefox), and it might be a good option for someone who’s not on WordPress or doesn’t want to mess with the functions file.

Basically, I type oyw and it expands to Organizing Your Way.

I type photosource and it expands to Photo by Name.

And so on.
Because I was a transcriptionist in my previous life, I have hundreds for everyday words as well. From that last sentence, I have bc = because, huns = hundreds, prev = previous and evd = everyday. It’s so bad that I find myself writing in text expanders even when I’m writing notes by hand!

[Reply]

Carrie Isaac Reply:

Mandi, I’ve considered using a text expander as well (and still would like to for other things like “memorizing” emails and what not). I definitely think some people would find it an easier alternative to setting up a shortcode.

I think one of the biggest advantages to setting up a shortcode rather than using a text expander is that you can edit the shortcode and *anywhere* it’s used on your site, it’s updated. So, if I, for whatever reason, want to use move my Coupons.com links from one affiliate to another, all I have to do is edit my shortcode and all my links change.

Or, sometimes there are offers that are available on an “on again, off again” basis. I’ve set up a shortcode for some of those, and when the affiliate link goes dead, I can go in and change the shortcode and it changes every place it’s on my site. If the affiliate offer goes back live, I can edit the shortcode, and voila, anywhere that link is on my site (via the shortcode), it’s got the affiliate link in it again.

My husband was supposed to buy me a text expander for Christmas and I’m not sure what happened… Need to follow up on that! ;)

[Reply]

Mandi @ Organizing Your Way Reply:

Definitely. That is a huge advantage!

[Reply]

Crystal Paine March 5, 2010 at 3:53 pm

Okay, you have totally and completely solidified forever for me that you are brilliant. But I’m pretty sure I already knew that. :) Now I’m just 150% more sure of that.

How can I twist your arm to write a post called, “Top 10 Ways to Get as Smart as Carrie”? Because, seriously, I’d totally pay some good money to read that post.

And I’m honored to be related to you… even if it is in some very distant way! ;)

[Reply]

Carrie Isaac Reply:

LOL, I’m not feeling so brilliant right now… Having major site problems, argh. But if I get it fixed you will be 175% sure of my smartness. ;)

[Reply]

Amber March 5, 2010 at 4:03 pm

I am sooooo glad you posted this! I have been searching for a way to do this for months and every single plug-in I found that was *supposed* to work didn’t end up linking! Thank you! I’m gonna try it tonight and hope that I don’t break anything :-)

[Reply]

Carrie Isaac Reply:

Haha, just back up and you’ll be fine! I, too, spent tons of time looking for a plugin and never found one that worked. After I figured out how to do this, I realized that it’s really quite easy, doesn’t require a plugin (which could break, become outdated, or slow down your site), and you have *complete* control over and actual understand what’s doing!

I’ve enjoyed using WordPress even more once I’ve started to get more comfortable with the geeky stuff!

[Reply]

Carrie Isaac Reply:

By the way, not sure if I’ll be online tonight or not, but you can tweet me if you have questions. :) @carriegisaac

[Reply]

Sherri @ Luv a Bargain March 6, 2010 at 8:34 am

Thank you, thank you!! I am SO excited! I have been wondering how to do this and didn’t know where to begin. I’m going to work on it later today and, like Amber, hope I don’t break anything :)

[Reply]

marybeth @ www.babygoodbuys.com March 6, 2010 at 2:29 pm

Thank you so much! I’m not into coding so much, but this is so rediculously useful I’m going to start! I’ll be messing with it in the next few days. You might get a tweet or two from me :)

[Reply]

Joelle March 6, 2010 at 10:03 pm

Thank you so much! I’ve been looking for this everywhere and the only wordpress plugin I found asked for money! So again, Thanks a Bunch! I added a couple of things to my code. I personally like it when a new window opens once the reader is redirected to a new website or blog. Also, I like to add “no follows” for any links that leave my website but don’t belong to fellow bloggers. Hence, here’s the code I use now (I used the redplum.com website as an example):

function redplumdotcom() {
return ‘RedPlum.com‘;
}
add_shortcode(‘redplum.com’, ‘redplumdotcom’);

Thanks,
Joelle

[Reply]

Joelle Reply:

I guess… it won’t show the full code… anyway. I added a

the no follow here:

return ‘RedPl…..

[Reply]

Carrie Isaac Reply:

Yes yes yes! I add rel=”nofollow” title=”disclosure: affiliate link” on many of my affiliate shortcodes.

By the way, can’t remember if I mentioned this, but you can do it with an image, too – so, if you frequently use a Safeway logo, you could create a shortcode for that.

Not sure if you can put anything other than plain text or HTML inside a shortcode.

[Reply]

Danielle March 8, 2010 at 2:23 pm

Amazing! Thank you so much Carrie! This is truly going to be a life and time saver! You rock! So-where did you learn so much about coding/html and what not? Any good reads you suggest for a newbie?

I will definately be tackling this project this week-Thank you!

[Reply]

Carrie Isaac Reply:

My favorite book is called Copy And Paste Until It Works. ;)

[Reply]

Leave a Comment

Previous post:

Next post: