1. Home
  2. ReHub Theme
  3. Affiliate settings
  4. How to add user ID to affiliate link as subid

How to add user ID to affiliate link as subid

From 7.9.2 version of theme, you can assign subid with user login name without any custom code. Go to Theme option – Affiliate – Cashback option and enable this function

Sometimes, you will need to add user ID to affiliate link. Usually, this can be useful if you want to make any kind of cashback or statistic for user.

Here code, add it to functions.php of child theme or grandchild plugin

add_filter('rh_post_offer_url_filter', 'rhlink_add_subid');
function rhlink_add_subid($offer_post_url){
    if (is_user_logged_in()) {
        $user_id  = get_current_user_id();
        $offer_post_url = $offer_post_url.'&subid='.$user_id;
    }
    return $offer_post_url; 
}

userID will be added in end of all affiliate links on your site in the end with example &subid=22

If you need another slug for subid, just change subid in code to own slug.

This code will work for all affiliate buttons of theme, so, you must use one of affiliate functions (it will not work if you use third party plugins with own buttons)

P.s. If your links are ended with trailing slash due to structure of deeplink and doesn’t have any url parameter (link doesn’t have symbol “?”) you must use another code

add_filter('rh_post_offer_url_filter', 'rhlink_add_subid');
function rhlink_add_subid($offer_post_url){
    if (is_user_logged_in()) {
        $user_id  = get_current_user_id();
        $offer_post_url = rtrim($offer_post_url,'/');
        $offer_post_url = $offer_post_url.'?subid='.$user_id;
    }
    return $offer_post_url; 
}

Or, if you want to add author name as subid

add_filter('rh_post_offer_url_filter', 'rhlink_add_subid');
function rhlink_add_subid($offer_post_url){
    if (is_user_logged_in()) {
        global $post;
        $author_id = $post->post_author;
        $user_id = get_the_author_meta('nickname', $author_id);
        $offer_post_url = rtrim($offer_post_url,'/');
        $offer_post_url = $offer_post_url.'?subid='.$user_id;
    }
    return $offer_post_url; 
}

Instead nickname, you can add any user field