Nathaniel Ward

Creating Clean Amazon Affiliate Links in WordPress

One way bloggers can earn incremental income is through affiliate marketing programs, among which Amazon’s is perhaps the most popular. Bloggers simply link to Amazon products, adding a special referral code, and they are given credit for the sales they generate.

Generating these tracking codes can be something of a pain. Amazon’s own utility is clumsy and offers up a long, clunky URL. Ragaskar’s WordPress plugin aims to fill the void by automatically appending tracking codes to Amazon links in posts, but this can still generate clunky links.

Based on Ragaskar’s plugin, I whipped up the following code:


    add_filter('the_content','filter_amazon_associate_filter');

    add_filter('comment_text','filter_amazon_associate_filter');

    

    function filter_amazon_associate_filter($content) {

        $affiliate_code="YOURCODEHERE";

    

        $content=preg_replace(

            '/http:\/\/[^>]*?amazon.([^\/]*)\/([^>]*?ASIN|gp\/product|exec\/obidos\/tg\/detail\/-|[^>]*?dp)\/([0-9a-zA-Z]{10})[a-zA-Z0-9#\/\*\-\?\&\%\=\,\._;]*/i',

            'http://www.amazon.$1/exec/obidos/ASIN/$3/'.$affiliate_code,

            $content

        );

        return $content;

    }

Very simply, this code scans through your posts and comments and replaces ugly Amazon links with somthing like http://www.amazon.com/exec/obidos/ASIN/B0048EJXCK/nathward-20.

To add this to your site, simply place the above code in your WordPress template’s functions.php file and replace YOURCODEHERE with your Amazon affiliate code.