Affiliate Referrer – WordPress shortcode

affliate referrerIf you are an affiliate and want to know which affiliate has referred you, you need affiliate referrer custom shortcode.

It applies only if you use Affiliates Enterprise plugin from itthinx. Once the affiliate has logged in with his/her user credentials, he will be able to see who is his/her referrer.

Affiliate Referrer – Usage

The shortcode can be used like this: [affiliate-referrer-info].

This will display the user_login of the affiliate referrer.

The shortcode’s accepted parameters can be user_login, user_nicename, user_email, user_url and display_name. You can use the parameters like this: [affiliate-referrer-info display=”user_login”].

Snippet

You can add the following snippet in your child theme’s functions.php file. Then add [affiliate-referrer-info] shortcode in a page to render info for your affiliate referrer.

Or alternatively you can get it from GitHub.

add_action ( 'init', 'add_affiliate_ref_info_shortcodes' );

function add_affiliate_ref_info_shortcodes ( $data ) {
	add_shortcode ( 'affiliate-referrer-info', 'affiliate_referrer_info' );
}

function affiliate_referrer_info ( $attr = array(), $content = null ) {
	global $wpdb;
	
	$options = shortcode_atts(
		array(
			'direct'  => false,
			'display' => 'user_login'
		),
		$attr
	);
	extract( $options );
	$output = '';
	$relations_table = _affiliates_get_tablename( 'affiliates_relations' );	
	$user_id = get_current_user_id();
	
	if ( $user_id && affiliates_user_is_affiliate( $user_id ) ) {
		if ( $affiliate_ids = affiliates_get_user_affiliate( $user_id ) ) {
			foreach ( $affiliate_ids as $affiliate_id ) {
				if ( $affiliate_referrer = $wpdb->get_var( $wpdb->prepare (	"SELECT from_affiliate_id FROM $relations_table WHERE to_affiliate_id=%d ", $affiliate_id ) ) ) {
					continue; 
				}
			}
		}
	}
	if ( $user_id = affiliates_get_affiliate_user( $affiliate_referrer ) ) {
		if ( $user = get_user_by( 'id', $user_id ) ) {
			switch( $display ) {
				case 'user_login' :
					$output .= $user->user_login;
					break;
				case 'user_nicename' :
					$output .= $user->user_nicename;
					break;
				case 'user_email' :
					$output .= $user->user_email;
					break;
				case 'user_url' :
					$output .= $user->user_url;
					break;
				case 'display_name' :
					$output .= $user->display_name;
					break;
				default :
					$output .= $user->user_login;
			}
			$output = wp_strip_all_tags( $output );
		}
	}
	
	return $output;
}

Affiliate Referrer on GitHub

Your suggestions, features etc are welcome. You can comment here or fork the repository on GitHub.

Note that…

…it is recommended to enclose the shortcode with [affiliates_is_affiliate][affiliate-referrer-info][/affiliates_is_affiliate].

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *