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.
1 Answer
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