Auto-activate affiliates on signup

I'm trying to automatically activate affiliates so they don't have to manually enable affiliate links on signup.

In this thread I found the below code snippet:

function custom_activate_afiliates() {
  $current_user = wp_get_current_user();
  $user_ID = $current_user->ID;
  if ( get_user_meta( $user_ID, 'enable_affiliate', true ) != 'yes' ) :
    update_user_meta($user_ID, 'enable_affiliate', 'yes' );
  endif;
}
add_action( 'wp', 'custom_activate_afiliates' );

This enables the affiliate links on signup, but the affiliate links aren't properly enabled. The affiliate URL shows as "?ref=" instead of "?ref=username-XXX". In order to fully enable the affiliate links users have to click "Update Settings" which defeats the purpose.

It seems like the above code snippet gets most of the way there and there's just a small error, but I can't figure out what it is. Any ideas?

Thanks!

  • aristath

    Hello there @Gabe, I hope you're well today!

    I looked at the database to see how the affiliate ID is generated but unfortunately I could not figure this out... I have notified the plugin developer about this, hopefully he'll be able to help us out on this one.
    Please keep in mind though that plugin developers have a lot of responsibilities so this might take a bit longer than a normal ticket.

    Cheers,
    Ari.

  • Paul

    @Gabe,

    Yeah, that other snippet does not consider the important 'affiliate_reference' which is the unique affiliate reference needed for the user's URLs.

    I've added the needed logic to the snippet to make it complete.

    function custom_activate_afiliates() {
    
       $user_ID = get_current_user_id();
       if ($user_ID > 0) {
          if ( get_user_meta( $user_ID, 'enable_affiliate', true ) != 'yes' ) {
             // Check if the user has an existing 'affiliate_reference' meta
             $affiliate_reference = get_user_meta( $user_ID, 'affiliate_reference', true );
             if (empty($affiliate_reference)) {
                $current_user = wp_get_current_user();
                $affiliate_reference = aff_build_reference( $current_user );
                if (!empty($affiliate_reference)) {
                   update_user_meta($user_ID, 'affiliate_reference', $$affiliate_reference);
                  update_user_meta($user_ID, 'affiliate_hash', 'aff' . md5(AUTH_SALT . $affiliate_reference));
                }
             }
             update_user_meta($user_ID, 'enable_affiliate', 'yes' );
          }
       }
    }
    add_action( 'wp', 'custom_activate_afiliates' );
  • Gabe

    Hi @Paul, thanks for the quick reply.

    I let affiliates register on the front-end using Gravity Forms and their user registration add-on. Using the code snippet you provided generates this error:

    Fatal error: Cannot use object of type WP_Admin_Bar as array in /nas/wp/www/staging/jac736isbw/wp-includes/wp-db.php on line 1459

    If I refresh the page after that error appears, everything works as normal, so it just seems temporary, but it's consistent. I've tested this by creating a number of new user accounts.

    Additionally, when logging in affiliate links still end with "?ref=" without including username-XXX until "Update Options is fixed" so it's still the same problem as before. Any ideas?

  • Paul

    @Gabe,

    I let affiliates register on the front-end using Gravity Forms and their user registration add-on.

    I really have no idea what this does or means.

    Using the code snippet you provided generates this error:

    Not really sure. The code I provided really has direct access or use to the WP_Admin_bar. Maybe some other plugin you are using is conflicting with the code. Regardless, I'd say you should probably just not use the code and go back to allowing users to opt-in to becoming an affiliate as per its design.

  • Paul

    @Gabe,

    I really don't have the full details of what you are attempting. I was just going off the code snippet you asked about. I say it and noticed it did not have any checks for the 'affiliate_reference' usermeta field. So the code I provided checks that and sets if needed. Other than that I don't know how you are obtaining the value for the affiliate URL or where you are testing this.

    I test the little but of code this morning and was able to verify when the page loaded that a non-affiliate user was properly set as an affiliate with the proper user_meta values.

  • Gabe

    @Paul

    No problem, I appreciate it. Affiliates is a powerful tool, so I used it to create a stand alone affiliate site so users can signup on that site just to become affiliates.

    I have a signup form on the homepage of the site that lets them create user accounts. Once they create an account and go to the dashboard everything works the same. I was just trying to make it so they don't have to manually enable affiliate links since the only reason they signup up is to be an affiliate.

    it may just be a weird issue on my installation, it's no problem and it's not a deal breaker.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.