Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Create and Manage Affiliates based on membership level when using AffiliateWP and Paid Memberships Pro
Creates an affiliate for new members of Level ID 1.
Updates affiliate status if the affiliate already exists.
Sets affilaite status to 'inactive' if membership of Level ID 1 is cancelled (includes expiration).
Add this code to a custom plugin.
function my_pmpro_affiliatewp_after_change_membership_level( $level_id, $user_id, $cancel_level ) {
//make sure affiliatewp is active
if($level_id == 1) {
//New member of level 1. Set up the Affiliate
if( affwp_is_affiliate( $user_id ) ) {
$affiliate_id = affwp_get_affiliate_id( $user_id );
//Affiliate already exists. Update Affiliate status to 'active'.
affwp_update_affiliate( array( 'affiliate_id' => $affiliate_id, 'status' => 'active' ) );
} else {
//Affiliate does not exist. Create the Affiliate.
affwp_add_affiliate( array( 'user_id' => $user_id, 'status' => 'active' ) );
if($cancel_level == 1) {
//User is cancelling or membership is expired.
if( affwp_is_affiliate( $user_id ) ) {
$affiliate_id = affwp_get_affiliate_id( $user_id );
//Affiliate exists. Update Affiliate status to 'inactive'.
affwp_update_affiliate( array( 'affiliate_id' => $affiliate_id, 'status' => 'inactive' ) );
add_action('pmpro_after_change_membership_level', 'my_pmpro_affiliatewp_after_change_membership_level', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.