1. Home
  2. ReHub Theme
  3. User submit, memberships, profiles
  4. Auto adding affiliate links to user submitted offers

Auto adding affiliate links to user submitted offers

If you use user submit with our RH Frontend PRO plugin, maybe you want to automatically add your affiliate tag to user submitted links. Here some code tips how to do this.

We added new plugin which can be used for the same task without coding skills. RH Cloak. You can download it from Rehub – Plugins

Read docs, how to use Rewrite Affiliate Links on site

But if you have basic code skills, better to use code snippet below

For example, you want to change amazon links.

Usually, affiliate links have next look

https://www.amazon.com/gp/aws/cart/add.html?ASIN.1=B00FJRP91Y&Quantity.1=1&tag=wpsoul05-20&medianet=222

amazon add link parameter “tag=wpsoul05-20” to make link as affiliate. So, you want to change link to own tag ID. Also, sometimes, links can be affiliate tag at all, so, maybe want to add your tag to link. This code will help you

add_action('wpfepp_form_actions', 'link_change_custom');
function link_change_custom($data){
    if ( !empty( $_POST['rehub_offer_product_url'] ) ) {

        $url = $_POST['rehub_offer_product_url'];

        $checkdomain = 'amazon.com';
        if (!empty($url) && strpos($url, $checkdomain) !== false) :

            $afftag = 'myafftag-02'; //our affiliate ID
            $affstring = 'tag='; // url parameter for affiliate ID

            if (parse_url($url, PHP_URL_QUERY)): //check if link has query string
                if (strpos($affstring, $url) !== false) : //check if link already has affiliate ID
                    $url = preg_replace("/(".$affstring.").*?(\z|&)/", "$1".$afftag."$2", $url);
                else:
                    $url = $url.'&'.$affstring.$afftag;
                endif;   
            else:
                $url = $url.'?'.$affstring.$afftag;
            endif;

        endif;  

        update_post_meta( $data['post_id'], 'rehub_offer_product_url', esc_url($url) );
    }     
}

Some explanation.

 

in $checkdomain you place domain which will be checked. In our case it’s amazon.com. Other links will be ignored

in $affstring you need to add query string parameter which is used in your affiliate network. This string is common for all users.

in $afftag you can place your affiliate ID which you want to change in link or add if it’s empty

This code works for RH Frontend PRO plugin. You need to add code to functions php of child theme or in grandchild plugin. Use correct way to customize theme

There is also another way. For example, you can offer users to leave their affiliate links and replace their affiliate ID on yours only, for example, in 20% of time. So, you will need to leave their links as is and save them on site, but, change link in time when it’s visible on site. You can use another filter of theme for this. This is basic code to change affiliate link to yours

add_filter('rh_post_offer_url_filter', 'link_change_custom');
function link_change_custom($offer_post_url){

        $checkdomain = 'amazon.com';
        if (!empty($offer_post_url) && strpos($offer_post_url, $checkdomain) !== false) :
            $afftag = 'myafftag-02'; //our affiliate ID
            $affstring = 'tag='; // url parameter for affiliate ID

            if (parse_url($offer_post_url, PHP_URL_QUERY)): //check if link has query string
                if (strpos($affstring, $offer_post_url) !== false) : //check if link already has affiliate ID
                    $offer_post_url = preg_replace("/(".$affstring.").*?(\z|&)/", "$1".$afftag."$2", $offer_post_url);
                else:
                    $offer_post_url = $offer_post_url.'&'.$affstring.$afftag;
                endif;   
            else:
                $offer_post_url = $offer_post_url.'?'.$affstring.$afftag;
            endif;
        endif;  

        return $offer_post_url;
    
}

If you need to check and replace several domains, this is example of another example which can be used to check several domains and affiliate IDs

function rh_change_affiliate_string($offer_post_url, $checkdomain, $afftag, $affstring){
    if (!empty($offer_post_url) && strpos($offer_post_url, $checkdomain) !== false) :
        if (parse_url($offer_post_url, PHP_URL_QUERY)): //check if link has query string
            if (strpos($affstring, $offer_post_url) !== false) : //check if link already has affiliate ID
                $offer_post_url = preg_replace("/(".$affstring.").*?(\z|&)/", "$1".$afftag."$2", $offer_post_url);
            else:
                $offer_post_url = $offer_post_url.'&'.$affstring.$afftag;
            endif;   
        else:
            $offer_post_url = $offer_post_url.'?'.$affstring.$afftag;
        endif;
    endif;  

    return $offer_post_url;	
}

add_filter('rh_post_offer_url_filter', 'link_change_custom');
function link_change_custom($offer_post_url){
  $offer_post_url = rh_change_affiliate_string($offer_post_url, 'aliexpress.com', 'myafftag-02', 'pid=');
  $offer_post_url = rh_change_affiliate_string($offer_post_url, 'amazon.com', 'myafftag-02', 'tag=');
    return $offer_post_url; 
}

Each affiliate link has several common things. They are: domain, affiliate query parameter and affiliate ID. For example: http://dl.flipkart.com/dl/nikon-d7200-body-af-s-18-105-mm-vr-lens-dslr-camera/p/itmemv3atvdhh2dk?pid=DLLEMV3AUXUFJEWM&affid=aksoni84g

As you see dl.flipkart.com is domain of such link, so, set it instead of aliexpress.com in my code. affid=aksoni84g  is affiliate part of link, you will not get commission without it. So, aksoni84g is affiliate ID, place it instead of myafftag-02. Other affiliate users will have the same link, but affiliate ID will be another, so, we will change it. affid= is query parameter, in most cases it’s before affiliate ID. Place it instead of pid= Don’t place query string parameter with symbols ? or &

And last one. Code will change affiliate id to your own only 20%

function rh_change_affiliate_string($offer_post_url, $checkdomain, $afftag, $affstring){
    if (!empty($offer_post_url) && strpos($offer_post_url, $checkdomain) !== false) :
        if (parse_url($offer_post_url, PHP_URL_QUERY)): //check if link has query string
            if (strpos($affstring, $offer_post_url) !== false) : //check if link already has affiliate ID
                $offer_post_url = preg_replace("/(".$affstring.").*?(\z|&)/", "$1".$afftag."$2", $offer_post_url);
            else:
                $offer_post_url = $offer_post_url.'&'.$affstring.$afftag;
            endif;   
        else:
            $offer_post_url = $offer_post_url.'?'.$affstring.$afftag;
        endif;
    endif;  

    return $offer_post_url;	
}

add_filter('rh_post_offer_url_filter', 'link_change_custom');
function link_change_custom($offer_post_url){
$randnumber = rand(1,5);
if ($randnumber == 1){
  $offer_post_url = rh_change_affiliate_string($offer_post_url, 'aliexpress.com', 'myafftag-02', 'pid=');
  $offer_post_url = rh_change_affiliate_string($offer_post_url, 'amazon.com', 'myafftag-02', 'tag=');
}
return $offer_post_url; 
}