Creating a Affiliate Link Cloaking Script

To my worries, my site which I have been running on my own plugin for shareasale datafeed was actually exposed with affiliate links all around my site. I was trying to find a way out to mask the affiliate links with affiliate cloaker, but how much one can create affiliate links if the number of products are 4k plus.

Hence I thought why not I give a try to code a Affiliate jump script. It was not so cumbersome after all.

The steps I did to code the script are as below.

1. get the Product id of the product from the single post page

2. Get the post id of the product in post_meta

3. get the Link for that post id in the post meta

4 create a href location jumper (redirector I mean).

Here goes the script to use in your sites (Don’t Forget to include this within the <?php –code—?> tags, if you happen to use this.I then modified the URL linking code in my shopperpress theme to accomplish the effective cloacking using my script below.

function redirect($url){
    if (!headers_sent()){
        header('Location: '.$url); exit;
    }else{
        echo '';
        echo ''; exit;
    }
}

require('./wp-blog-header.php');
global $wpdb;
$wpdb->show_errors();
ob_start();
$prodId=$_GET['productID'];
$meta_key1 = 'affiliate_productID';
$post_id=$wpdb->get_var($wpdb->prepare("SELECT post_id FROM
$wpdb->postmeta WHERE meta_key ='". $meta_key1."' and meta_value='".$prodId."'"));
$meta_key2 = 'link';
$link=$wpdb->get_var($wpdb->prepare("SELECT meta_value FROM
$wpdb->postmeta WHERE meta_key ='". $meta_key2."' and post_id='".$post_id."'"));
redirect($link);
ob_end_flush();
exit();

How to use this script ?
Well its simple, put the above code in root directory of your webshop/blog say goto.php,

Then call it on your products page like www.siteurl.com/goto.php?productID=123456

Simple isn’t it.

  • where and how i can call this file (goto.php). Please define complete method.