Needcoffee.com
PLEASE NOTE: “As an Amazon Associate, [Need Coffee] earns from qualifying purchases." You know we make money from Amazon links,
and I know you know this, but they make us say it anyway. More info, click here.

Quick and Dirty Amazon Links in WordPress

Sadly, there is no good Amazon plugin in WordPress that makes me happy. And you know what I’m talking about: a plugin where you never have to leave WordPress. You can look up an item, get the ASIN, link it up–all in the same window. The last Amazon plugin I used was cumbersome and its search was wacked: you could hardly ever find what you were looking for, even if it was a tremendously popular item.

So basically I’ve created three Amazon-related buttons in my quicktags to do the work. I still have to find the ASIN of the item I want (if I’m linking to an item) but that’s workable. If somebody had a Greasemonkey script that would make the ASIN show up in a search without making me open each item, that would be even better. But one of these days. Anyway, these three buttons do this:

1. Link to a specific item using slightly customizable anchor text.
2. Link to a subject rather than an item using slightly customizable anchor text.
3. Link any text to a specific item.

As always, remember that I’m a writer. I was telling someone the other day that I’m the coding equivalent of the Midnighter. There’s no art to what I do, I smack things with code until they start working. So your mileage may vary–keep a backup of all your files and a fire extinguisher handy.

[ad#shortpost]

First thing to do is find the quicktags.js file. It should be in wp-includes/js

Then find the edButtons section. The order that things appear in edButtons is the order they appear on your quicktags button bar thing. So bear that in mind.

You want to add the following edButton info, assuming that you want all three buttons I’ve defined above:

edButtons[edButtons.length] = 
new edButton('ed_amaz'
,'amazon item'
,''
,''
,'z'
,-1
); // special case

edButtons[edButtons.length] = 
new edButton('ed_amazsubj'
,'amazon subject'
,''
,''
,'j'
,-1
); // special case

edButtons[edButtons.length] = 
new edButton('ed_amazenclose'
,'amazon enclose'
,''
,''
,'a'
); // special case

Those go in order, 1-3, so if you don’t want one, leave it out.

Next you want to find the section that starts with this:

function edShowButton(button, i) {

And you want to add this…I’m breaking it up so it doesn’t break my theme here, but you’ll get the gist. If you’re concerned about the lines being broken and you’d feel better cutting and pasting from a different file, I’m putting this all in a flat text file here. Feel free to save that to your hard drive and follow along here.

	else if (button.id == 'ed_amaz') {
		document.write('');
	}
	else if (button.id == 'ed_amazsubj') {
		document.write('');
	}
	else if (button.id == 'ed_amazenclose') {
		document.write('');
	}

You want to add this before the end of that edShowButton section. The last bit of which should look like this:

	else {
		document.write('');
	}
}

If you put it below this, then you’ll screw yourself. So just don’t do that.

Below that you should see the section with all the functions in it. One by one, here they are. The first links to the specific item.

function edInsertAmaz(myField) {
	var myValue = prompt('Enter the ASIN', '');
	var myValue2 = prompt('Click here to buy ____ from Amazon.', '');
	if (myValue) {
		myValue = 'Click here to buy '
				+ myValue2
				+ ' from Amazon.';
		edInsertContent(myField, myValue);
	}
}

Now when you use this, you’ll be asked to Enter the ASIN for the item in question. Then it will prompt you to fill in the blank of “Click here to buy ___ from Amazon.” You can obviously change this to whatever you want, but I’ll say “it on DVD” so the linked text is “Click here to buy it on DVD from Amazon,” whatever it is. I think you can see that the var MyValue prompt bits are what you see on the admin side, whereas the output in your code is the MyValue below it. And if you screw around with it just make sure that you’ve got opening and closing single quotes. Notice also that I use double quotes inside the single quotes for my HTML code. So don’t mix and match.

Also, a quick note about the rel=”nofollow”. I nofollow links away from the site that don’t need my linklove. For example, I’m sending traffic to Amazon. They’re making money off of the links I send them (hopefully, if you click and buy, he said shamelessly) so I figure they’re getting value already. I’d much rather save linklove for somebody like, you know, Fantastic Blognanza or Occasional Superheroine. But you be the judge of whether or not you want to go that route.

And of course, YOURAMAZONASSOCIATECODE is your Amazon associate code. Fill it in yourself. Is probably not all caps.

This next one is to link to search results for a subject you specify on Amazon. So if I wanted to create a link to products involving fondue:

  • Click here to buy fondue stuff from Amazon.
  • 
    function edInsertAmazSubj(myField) {
    	var myValue = prompt('Enter the subject matter 
    (use a plus between words)', '');
    	var myValue2 = prompt('Enter the subject matter 
    (no plus this time and capitalized)', '');
    	if (myValue) {
    		myValue = 
    '
  • Click here to buy ' + myValue2 + ' stuff from Amazon.
  • '; edInsertContent(myField, myValue); } }

    Basically it prompts you first for the subject, formatted to go into the URL for the Amazon link, so for Star Wars that first one would be “star+wars” and the second one, which is what shows up in the text, is “Star Wars.” Some other things I’ve done is to put

  • and
  • on either side of the link because nine times out of ten I’m putting it at the bottom of a page and that’s just how we format things.

    Now I’ll mention that you should see “index=blended” in here, right? That tells Amazon to use their entire site to do the search. If you futz around with it, I believe you can do index=dvd, index=books and index=music or stuff like that. But play with it if your site is specific and see what works best and actually, you know, works. Also, the sort is “salesrank,” which puts the best sellers first in your results. If you want something else, you go play around on Amazon, see how they structure their URLs and swipe that.

    And here’s my favorite. This lets you create text, like this, and then highlight it, and link it to Amazon just like you would normally highlight it and bold it or italicize it or whatever.

    function 
    edInsertAmazEnclose(myField, i, defaultValue) {
    	if (!defaultValue) {
    		defaultValue = '';
    	}
    	if (!edCheckOpenTags(i)) {
    		var ASIN = prompt('Enter the ASIN', '');
    		if (ASIN) {
    			edButtons[i].tagStart = 
    '';
    			edInsertTag(myField, i);
    		}
    	}
    	else {
    		edInsertTag(myField, i);
    	}
    }

    So write some text like this, click the button, provide the ASIN and you’re good. This is particularly handy when you just happen to mention, you know, The Complete Calvin & Hobbes or an Ascaso DR.119 Dream 16-Bar-Pump Espresso Machine and you want to link them up.

    Now, some things to bear in mind. You will break your quicktags first try. So as long as you’ve got a backup, you’ll be fine and worst case scenario, only you and the people who post on your blog will see that there’s a problem–and the problem will be pretty damn obvious: your quicktags won’t be there. Check that your { and } are all where they should be, that your quotes match up and that you haven’t inadvertently called a variable AmazEnclose in one place and AmzEnclose in another.

    If you find any problems with my code here, let me know in the comments. And if you have any questions or, more likely, know how to do this better, please comment as well. But this is a quick and dirty way to include Amazon links, make money from them, and not have to use a plugin to do it.