Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to create an affiliate system on WordPress . What I mean is when "Member A" invite other to join (ex. "Member B") by using the ?referralId=MemberA querystring, then Member A get points or credits, and when Member B invite Member C, then Member B get points. Is there any WordPress plugin that can do that ?

I already tried Affiliate Pro Plus (http://wordpress.org/extend/plugins/affiliate-pro-plus/) but It's not work as expected. When I try to register Member B using Member A's referral, Member B does not count as Member A downline.

share|improve this question
    
Could you illustrate the need behind this? I'd be interested in which real world use case this could have. Thanks. –  kaiser Mar 30 '11 at 18:05
    
I want to create a website where member get credits/points by inviting other to register. Check themeforest.net, we can add ?ref=xxx on any url on themforest.net –  Permana Mar 31 '11 at 6:03
add comment

1 Answer

up vote 11 down vote accepted

Don't know about a plugin but this can be done easily with just two hooks and callback functions: First you add the refferer field to the registration form using register_form hook:

add_action('register_form','show_reff_field');
function show_reff_field(){ ?>
    <input id="ref" type="text" tabindex="20" size="25" value= "<?php if (isset($_GET['ref'])){echo $_GET['ref'];} ?>"  name="ref" readonly="readonly"/>
<?php
}

Then you just need to save it using user_register hook

add_action('user_register', 'register_refferal');

function register_refferal($user_id) {
   $userdata = array();
   $userdata['ID'] = $user_id;
   wp_update_user($userdata);
   $userdata['ref'] = $_POST['ref'];

   if (isset($userdata['ref'])  && !empty($userdata['ref']) && $userdata['ref'] != ""){
        //get reffering user id by his login
        $refuser = get_user_by('login',$userdata['ref']);
        //get current refferial credit that user has
        $current_ref_credit = get_user_meta($refuser->ID, 'ref_credit', true);
        //add credit for the newly created user
        $current_ref_credit[] = $user_id;
        //save the changes
        update_user_meta( $refuser->ID, 'ref_credit', $current_ref_credit);
   }
}

So all that is left to do is to let your users share there referral link:

http://example.com/wp-login.php?action=register&ref=my_login_name

So if my user name was "bainternet" then my referral link would be:

http://example.com/wp-login.php?action=register&ref=bainternet

and to see how many members are in a users downline here is a simple function which accepts a user ID and returns an array of user ids which he has reffered:

function get_user_downline($user_id){
    return $current_ref_credit = get_user_meta($user_id, 'ref_credit', true);
}

and its usage is simple:

// to echo count of how many user with ID of 24 as reffered:
echo count(get_user_downline(24));

    // to list the users user with ID of 24 as reffered:
<ul>
<?php
    $reffed = get_users(array('orderby' => 'registered', 'include' =>  get_user_downline(24));
    foreach ($blogusers as $user) {
        echo '<li>' . $user->display_name . '</li>';
    }
?>
</ul>

So just copy all of this code and you have a plugin for referral system

share|improve this answer
    
Thank you very much for the code provided. I actually trying to avoid coding just in case there are already a plugin that can do that. Your code is good start, now I need to add cookies support (where the ref is stored on cookies for 30 days) –  Permana Mar 31 '11 at 6:04
3  
Take a look at most plugins: They contain 5 lines of code and 50 of comments. What Bainternet wrote is perfect. Nothing more or less needed. –  kaiser Mar 31 '11 at 9:00
    
Excellant work Bainternet.try to implement as a plugin in wordpress site.it'll be helpful to all. –  Ramkumar Jun 29 '11 at 7:23
    
I have a WP setup where I have a custom pop up login box that users Social Login Plugin wordpress.org/extend/plugins/oa-social-login where a user can login with FB or Twitter. So a user never sees standard WP login form. I have tried to access $_GET within the user_register action, but it's always empty. Any tips on how it would be possible to get ref URL param with a setup like that? Many thanks. –  dashaluna Oct 24 '12 at 14:04
1  
@Bainternet thank you for explaining. I tried with a session but that didn't seem to work. I will dig into sessions more. Thanks again, I know it's hard to know the reason when you can't see a project.. really appreciate your tips! –  dashaluna Oct 24 '12 at 15:07
show 1 more comment

protected by Community Apr 1 '12 at 0:25

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.