How to Auto-activate Affiliates on signup

Hello,

I am using Pro-Sites + Affiliates.

How can I auto-activate affiliates for new users that sign up?

I found a snippet here:
https://premium.wpmudev.org/forums/topic/auto-activate-affiliates-on-signup

But I don't know where to put that?

  • Nick
    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' );

    It seems that the above code only works AFTER users have upgraded to Pro Site status? Could it be? For regular / free users they don't automatically get activated?

  • Nick

    After having done some testing, it gets weird.

    When Affiliates + Pro-Sites are activated with no pro-sites modules:
    It doesn't work

    When Affiliates + Pro-Sites (And Pay To Blog module) are activated:
    It works - but FIRST when the users attempts to go to his dashboard and gets directed to upgrade his Pro-Site membership.

    If I manually upgrade his membership to Pro BEFORE he logs into wp-admin - it doesn't work.

  • Michael Bissett

    Hey @Nick, hope you're doing well this evening! :slight_smile:

    Since you had mentioned on the live chat earlier today that you were trying some code from this thread over here:

    https://premium.wpmudev.org/forums/topic/how-can-users-be-automatically-affiliates#post-732940

    And that you were encountering errors with that code, I'd like to have a closer look at what's going on here. Could you please send in the following:

    - In the subject field add "Attn: Michael Bissett”
    - Link back to this thread
    - Include WordPress network admin access details
    - Include FTP log-in details
    - Include any relevant URLs for your site
    - Include any relevant details about what's happening here

    On the contact form, select "I have a different question", this ensures it comes through and gets assigned to me.

    https://premium.wpmudev.org/contact/

    Thanks! :slight_smile:

    Kind Regards,
    Michael

  • Michael Bissett

    Hey @Nick, thanks for your patience here! :slight_smile:

    While the FTP access details didn't seem to work for me (could you please make sure that they were entered properly?), I see what you mean in regards to the error that shows up when using that other code snippet.

    However, after thinking on this:

    When Affiliates + Pro-Sites (And Pay To Blog module) are activated:
    It works - but FIRST when the users attempts to go to his dashboard and gets directed to upgrade his Pro-Site membership.

    I'd like to ask, what to you mean by "It works - but FIRST when the users attempts to go to his dashboard and gets directed to upgrade his Pro-Site membership."? Could you elaborate on this please?

    As for this:

    If I manually upgrade his membership to Pro BEFORE he logs into wp-admin - it doesn't work.

    It would appear that you're bypassing whatever it is that the code snippet requires in order to register that user as an affiliate.

    The end result you're looking for is still that when a user registers for a site, he's made an affiliate automatically (without even being assigned a Pro Site level yet), right?

    Please advise,
    Michael

    • Nick

      Hello Michael,

      The thing is, if Pay to blog isn't activated. The plugin doesn't work at all.

      It only works when Pay to blog is activated, and it only works if the new users tries to go to his dashboard but gets redirected to the upgrade page from Pro-sites.

      If we upgrade the user before he tries going to his dashboard (giving him Pro status) - it doesn't work - as he would then not see the "Upgrade page" but get directly to his admin dashboard.

  • Hoang Ngo

    Hi @Nick,

    I hope you are well today.

    The old code seem not really active when a user sign up, it's active when user first login.

    I have some update for it, please try this code

    function custom_activate_afiliates($user_id) {
    
        $user_ID = $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( 'user_register', 'custom_activate_afiliates' );

    Best regards,
    Hoang Ngo