Home » Web

How to add an affiliate code automatically to an url

16 October 2009 594 views No Comment

If you have enrolled for affiliate programs like Commission Junction, Link Share, Shareasale etc. you may be finding it too time consuming to copy paste links for each product your are promoting in your blog. With a simple php script all the links going to a site can be stuffed with an affiliate code.

Once setup you do not have login to cj or link-share to get the link to the page you want to post. All you have to is just copy paste the link the page and script will stuff affiliate code when some one clicks on the link.

In order to build the links dynamically advertiser must support deep linking. Most advertisers do support deep linking. If the advertiser on CJ, linkshare allows to build custom links through a url then it supports deep linking. You can also stuff amazon associate code to any amazon.com page.

Create a file called affilate-link.php. Add following php code to this php file.

function generate_affiliate_url_from_url($url)
{
$encoded_url = $url;
$domain = parse_url($url, PHP_URL_HOST);
switch($domain)
{
/* cj affillliates */
case "www.6pm.com":
$encoded_url= "http://www.dpbolvw.net/click-3625568-10536655?url=";
break;

case "www.ninewest.com":
$encoded_url = "http://www.tkqlhce.com/click-3625568-10705932?url=";
break;
case "www.amazon.com":
$url=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/dp/$3/?tag=’.'fashionchicks-20′,$url );
return $url; /* credit for amazon code goes to Rajan Agaskar */
break;
/* add link share affiliates here */
default:
}
$encoded_url = $encoded_url.rawurlencode($url);
return $encoded_url;
}

?>

How to use the code

1. Copy the file to the same directory typically to the root folder
2. Include this php file to your script using

On wordpress installations add this to the bottom of header.php file of your theme.
3. Call this function from anywhere you want a url to a new url with affiliate coded embedded to it.
For example:

In wordpress this function can be called wordpress post carrying php code or any wordpress template files.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.