How To Create a Simple Affiliate Program in WordPress
One way I am promoting Pluginize.com is through our new Affiliate Program. While researching various affiliate programs to use I finally came to the conclusion that every affiliate program out there was way over bloated for what I needed. All I needed was a simple way to track a referral code when a user visits Pluginize from an affiliate link.
I created this simple WordPress affiliate program using three plugins:
Step 1: Account Registration
The first step in any affiliate program is to have your new affiliate create an account. To do this I simply enabled Membership in WordPress under Settings > General. I also set the default user role to Subscriber.
Now all new affiliates can register an account in WordPress: http://pluginize.com/wp-login.php?action=register
Step 2: Affiliate Referral Code
Next is where the WP Hide Dashboard plugin comes into play. After the new affiliate has registered and logged into WordPress I didn’t want them presented with the normal WordPress dashboard. Instead I wanted them directed to their Edit Profile page and hide everything else. The WP Hide Dashboard plugin does just that. Simply activate the plugin and any users that is set to the Subscriber user role will be redirected to their Edit Profile page. Everything else will be hidden.
Now that the user is viewing their Edit Profile page we need to add a few additional options. In this case I wanted to allow the user to set their own unique referral code and also their PayPal email address. To do this I had to create some custom code. I packaged up all of the custom code into a single plugin available at the bottom of this post. Now lets look at the code:
Add Additional Fields to Edit Profile
<?php
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) { ?>
<?php
//set the referral code if it exists, if not set to null
$referral_code = ( get_the_author_meta( 'referral_code', $user->ID ) ) ? esc_attr( get_the_author_meta( 'referral_code', $user->ID ) ) : NULL;
?>
<h3>Affiliate Program</h3>
<table class="form-table">
<tr>
<th><label for="referral_code">Referral Code</label></th>
<td>
<input type="text" name="referral_code" id="referral_code" value="<?php echo $referral_code; ?>" class="regular-text" /><br />
<span class="description">Please enter a unique affiliate referral code.</span>
</td>
</tr>
<tr>
<th><label for="twitter">PayPal Email</label></th>
<td>
<input type="text" name="paypal_email" id="paypal_email" value="<?php echo esc_attr( get_the_author_meta( 'paypal_email', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your PayPal email address. All affiliate payments are made via PayPal.</span>
</td>
</tr>
</table>
<?php
}
?>
As you can see from the image below our two additional profile fields are added to the Edit Profile page in WordPress.
Now that we’ve added our two additional fields we need to save them when the user updates their profile.
Save Additional Profile Fields
<?php
add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
function my_save_extra_profile_fields( $user_id ) {
global $wpdb;
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
//check if referral code already exists
$existing_code = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'referral_code' AND meta_value = '" .esc_html( $_POST['referral_code'] ). "';"));
if ( !$existing_code ) {
//update referral code
update_user_meta( $user_id, 'referral_code', esc_html( $_POST['referral_code'] ) );
}
//update PayPal email
update_user_meta( $user_id, 'paypal_email', esc_html( $_POST['paypal_email'] ) );
}
?>
Now our custom referral code and PayPal email are stored as user meta data for each user in WordPress. Easy as that!
Next lets add a message to the top of the Profile page instructing our affiliates on what to do:
Add Message to Profile Page
<?php
add_action('personal_options', 'affil_msg');
function affil_msg( $user ) {
if ( !get_the_author_meta( 'referral_code', $user->ID ) ) {
//if the referral code doesn't exist instruct the user on how to save it
echo '<div id="message" class="updated">';
echo '<p><strong>Thank you for joining Pluginize.com! To join our affiliate program set a referral code below. This code will be used to track any sales generated from your affiliate links</strong></p>';
echo '</div>';
} else {
//if the referral code DOES exist show the user what their affiliate URL is
echo '<div id="message" class="updated">';
echo '<p><strong>Affiliate URL: <a href="http://pluginize.com/?referral=' .esc_html( get_the_author_meta( 'referral_code', $user->ID ) ). '" target="_blank">http://pluginize.com/?referral=' .esc_html( get_the_author_meta( 'referral_code', $user->ID ) ). '</a></strong></p>';
echo '</div>';
}
}
?>
The above code will add a message to the top of the WordPress Profile page. The message will change based on whether a referral code has been set or not
Step 3: Tracking the Referral Code
Now that our affiliates can set their referral code we need a simple way to track that referral. Pluginize.com is using Gravity Forms for our contact form plugin. To track affiliate sales I added a “Referral Code” single line field to our form. Under the fields advanced options I enabled “Allow field to be populated dynamically” and set the Parameter Name to “referral” as shown below:
Enabling this option will allow me to dynamically populate that field by passing my referral code as a querystring. For example if you visit http://pluginize.com/get-started/?referral=mycode the Referral Code fill will automatically be set to mycode. The problem with this method is I want to track the referral on any page of the website. To do this I developed some code to track the referral code in a cookie:
Store Referral Code in a Cookie
<?php
//if a referral code exists and the cookie does not, create the cookie
if ( $_GET['referral'] && !isset( $_COOKIE['referral'] ) ) {
setcookie('referral', esc_html( $_GET['referral'] ), time()+3600*24*30 ); //saved for 30 days
}
?>
The above code will save the referral code in a cookie for 30 days, regardless of what page the user visits first. Now that we have the cookie set we need to get Gravity Forms to detect it when we visit our contact form.
<?php
add_action('init', 'check_affil_cookie');
//check if cookie exists
function check_affil_cookie() {
if ( isset( $_COOKIE['referral'] ) && strpos( $_SERVER['REQUEST_URI'], 'get-started' ) > 0 && !$_GET['referral'] ) {
//if cookie is set and we are visiting the "get-started" page redirect to the form
wp_redirect( add_query_arg( 'referral', $_COOKIE['referral'] ) );
}
}
?>
The above code checks if a cookie exists containing the referral code. If it does and the user visits the “Get Started” page on Pluginize we automatically add the referral code to the querystring which dynamically populates the referral code form field.
That’s it! I now have a very simple affiliate program in WordPress. This allows me to track referrals using a 30-day cookie. If any contacts come in that have a referral code we know exactly who to pay if we end up doing the work.




