Blog Tip

HOWTO: Automatically Create Affiliate Links

In my previous blog template programming posts I have concentrated on adverts. You can make money on your blog from straight links too, though it can be laborious adding them in. This is where todays piece of code comes in, using this snippet you can automagically convert product names into affiliate links.

This code should work with any PHP based blog but you will have to do some digging to see where and how the content is presented. For this post, as usual, I am concentrating on Drupal and WordPress.

The Process

To do the search and replace we will create a list of phrases and their associated affiliate link HTML. When the content is displayed we will replace all the mentions of our phrases with their affiliate link representation.

For examples I have used product links from my photography blog, simply replace the phrases and links with your own and add to the list as necessary.

The first array ($find) contains the phrases to look for, entered as regular expressions that tell it to look for our phrase surrounded by word boundaries (so it doesn’t look within words) and to look case insensitively. So the first example is ” canon 350d “. Next we have a $replacement array that contains the link html for each entry in the first array.

If you want to add to these lists simply follow the same format (remember to put commas in where they are needed) and ensure each phrase has a matching link entry.

Drupal Template Hack

Ordinarily in Drupal a variable is populated with the content of your post and this is sent to the browser. Very straightforward. The variable is displayed in the node template.

Our code takes the $content variable and does the search and replace before it gets printed to the browser.

Canon 350D“,
Nikon D70s“,
Nikon D50“,
);

$content = preg_replace($find, $replacement,$content);

print $content

?>

WordPress Plugin

In WordPress we do not have direct access to the content, there is a tag that does the display called the_content. Rather than hack this code we need to do the more elegant route which is to create a filter. Filters are plugins that when activated get executed on the content, usually used for profanity filters on user comments etc. This is perfect for our needs!

Paste the following code into a text file, save it as makelinks.php and upload it to your plugins directory. Once uploaded you can activate it from your plugins option in your blogs admin control panel.

Canon 350D“,
Nikon D70s“,
Nikon D50“,
);

$content = preg_replace($find, $replacement,$content);
return $content;
}

add_filter(‘the_content’, ‘makeafflinks’);

?>

The comments at the top of the code are read by WordPress when displaying available plugins, feel free to change to your own needs if you like. It is the same code pretty much as the Drupal example but with a command to add our new filter to WordPress.

Summary

You do not have to use this for affiliate links, you could use it to easily link to your own posts or to link to dictionary definitions etc. The implementation would be just the same.

Author: Chris Garrett

Chris Garrett is a content marketing and blogging coach and co-author of the Problogger Book with Darren Rowse.

82 thoughts on “HOWTO: Automatically Create Affiliate Links

  1. I’m continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would be no hungry people, and probably no litter either!

  2. I went to make a change and add in a way to fix my old, non-working affiliate links but when i do it, I get an error in the PHP and I’m not sure how to fix it. In the $find array I am adding this:

  3. ??????? ??????? ???



    ??????? ??????

    ??????? ???

    ??????

    ????? ??????? ??? 



    ????? ??????

  4. ????? ??????? ??? ? ??????? ??????? ??? ? ????? ????? ? ????? ???? ? ????? ??????? ? ????? ??? ?????? ? ????? ????? ? ????? ????? ? ????? ??????? ? ????? ????? ? ????? ??????? ? ????? ????? ?????? ? ????? ????? ????????? ? ??????? ??? ? ??????? ? ????? ??????? ??? ? ????? ??????? ? ????? ???????? ? ??????? ? ??? ?????? ? ????? ? ??? ? ??????? ? ????? ? ?????? ? ????? ?????? ? ????? ????????? ? ??????? ? ????????? ? ???????

    ????? ????? – ?????? ???? – ?????? ????
    ????? ????? – ?????? ?????? – ????? – ???? ??????
    ????? – ????? – ?????? ????? – ??? ????? – ??? ??????? ?????
    ???????? , ?????????
    ????? – ????? – ?????? ????? – ??? ????? – ??? ??????? ?????
    ?????? ????? – ?????? ???? – ?????? ?????
    ??? ?????? – ????? – ????? ??????
    ????? ????? – ??? ??????? ??????? ?? ??? ??????
    ??? – ?????? – ??????
    ?????? ???? – ??? ?? ???? – ????? ???? – ?????? ???? – ????????? ????
    ??? ??????? – ????? ??????? ??????? – ????? ????
    ????? – ????? ?????? – ???? ??????
    ????? ?????? – ??????? – ?????? – ??????
    ????? ?????? – ?????? ?????? – ????? ??????? – ??? ??????? – ????? ???????
    ?????? – ??? ?????? – ?????? ?????
    ??? ?? ??? – ????? ??? – ?????? ???
    ??? – ??? ????? – ??? ????? – ??? ????? – ????????
    ????? – ?????? – ??????? – ?????
    ??? – ????? ????? – ?????
    ????? – ????? ??????? – ????? ?????????? – ????? ???????
    ??? ????? – ????? – ??? – ?????? – ???? – ????? – ?????? ???????
    ????? ???? – ????? ???? – ????? ???? – ??????? ????
    ?????? – Bluetooth – ????? ??????
    ??????? SMS ? MMS ????? MMS – ????? SMS
    ??? ?????? – ??? ??? – ???????? ????? ????????
    ??????? ?????? – ??????? ?????? ????? – ???? ?????? – ??????? ??????

  5. ????? ???? – ????? – ???? – ??? – ????? – ??????? – ??????? ????? – ?????? ???? ???????? – ??????? ??????? – ??????? – ????? ??? – ??????? ??????? – ????? ?????? ??? – ????? ??????- ?????? ???? – ?????? ???????? – ??????? ??????? – ?????? ????? – MMS – ?????? – ????? 2009 – ????? 2009 – ?????? – ?????- windows live messenger – ??????? ?????? – ??? ????? – realplayer vista- ???? ????? – ??? ?????? – ?????? ????? – ??? ???? ???????? – , ?????? ????? – , ??????? ????? ????? – ???? ???????? – ???? ?????? ???????? – ??????? ?????? ???????? – ??? ???? ????? ???????? – ????? ???????? – ?? ????? ???? ???????? ????? – ?????? ???????? ?????????? – ?????? ???????? ??????? ???????? – ???? ????? ???????? – ????? ???????? – ??? ???????? – ???????? ???????? – ???????? ???????? – ??? ?????? ???????? – ???? ?????? – ????? ?????? ???????? – ???? ???? ????? – ???? ????? ?????????? – ???? ???????? – ???? ????? – ??? ???????? – ???? ???????? – ?????? ???????? – ?? ??? ???????? – ???? ???????? – ??? ?????? ???????? – ??? ???? ???????? – ??? ???????? ???????? – ????? ???????? – ??? ???? – ??? ????? ???? ???????? – ?????? ???????? – ?? ??? ???????? – ???? ?????? – ???? ???????? ????? – ???? ???????? ?????? – ?? ???????? – ???? ????? – ????? ????????

    ????? ????
    ?????
    ????
    ???
    ?????
    ???????
    ??????? ??????????? ???? ????????

  6. I’m continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would be no hungry people, and probably no litter either!

  7. ????? ???? ???????[www.halamry.com]

    ??????? ?????[www.q1rr2.com]

    ???? ???????[www.halamry.com]

    ?????[www.q1rr2.com]

    ?????[www.halamry.com]

    ???????[www.q1rr2.com]

  8. ????? ???? ???????[www.halamry.com]

    ??????? ?????[www.q1rr2.com]

    ???? ???????[www.halamry.com]

    ?????[www.q1rr2.com]

    ?????[www.halamry.com]

    ???????[www.q1rr2.com]

  9. Thank you very much

    I’m continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would be no hungry people, and probably no litter either!

    ????? ???? ???????www.halamry.com]

    ??????? ?????www.q1rr2.com]

  10. I’m continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would be no hungry people, and probably no litter either!

  11. I’m continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would be no hungry people, and probably no litter either!

    ยป
    ?????
    ????

  12. continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would
    ???? ???? ????? ???? ??????? ???? ????

  13. continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would be no hungry people, and probably no

  14. continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would be no hungry people, and probably no

  15. I’m continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would be no hungry people, and probably no litter either!



  16. I’m continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would be no hungry people, and probably no litter either!

  17. I’m continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would be no hungry people, and probably no litter either!

  18. I’m continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would be no hungry people, and probably no litter either!

  19. I went to make a change and add in a way to fix my old, non-working affiliate links but when i do it, I get an error in the PHP and I’m not sure how to fix it. In the $find array I am adding this:

    '/\bwww.shareasale.com/r.cfm?u=150395&b=11317&m=3465&afftrack=&urllink=www.groovycandies.com\b/i',

    and in the $replace array I am adding this:

    "www.groovycandies.com",

    to un-affiliatize those links but I get an error saying “Warning: preg_replace() [function.preg-replace]: Unknown modifier ‘r'”

    so i think it’s the /r in the $find string but I don’t know how to fix it. All of my other find strings do not have slashes in them.

    HELP!

    brian

  20. Here was my problem:
    I had LOTS of affiliate links to a SAS and they dropped out of the SAS aff program. Now, all my aff links go to a SAS error page (not sure why they don’t redirect to the proper page after 5 seconds or something).

    So, I’m stuck with all of these dead links in my site now. With a little tweaking, I modified this makelinks plugin to handle all my affiliate links automatically from now on. Now, if an aff leaves the program, I just make one change to the makelins.php and all my links still work.

    The only problem now is that all the old/dead links still don’t work. I will have to go back into the old ones and modify the links to my new link format (I am now appending afflink: to the links to differentiate them – otherwise, this plugin will relink my already done aff links – which will break them).
    so, my new link format in the blog posts is:

    at Candy Direct

    and the rule looks like this:

    $find = array('/\bafflink:www.candydirect.com\b/i'
    $replacement = array(
    "www.candydirect.com/cgi-bin/affiliates/clickthru.cgi?id=candyaddict&page=http://www.candydirect.com",

    and that makes the new blog code look like it should:

    http://www.candydirect.com/cgi-bin/affiliates/clickthru.cgi?id=candyaddict&page=http://www.candydirect.com/novelty/Pop-Rocks-Chocolate.html

    So, going forward, all my links will work as planned and I will now use this for all future affiliate programs I use/join up with. The downside is all those old links still have to be recoded by hand. The upside is that will never happen again.

  21. I’m continually impressed with the free contributions people make to ‘our community’. If only we did this in real life. There would be no hungry people, and probably no litter either!

Comments are closed.