• Resolved Michael

    (@mischnu)


    Is there a way to disable an invitation code only after having successfully verified a user’s email address?

    My use case: I use one-time invitation codes that I hand out to new users, registration is invitation-only. I also want to verify the new user’s email address. Now the worst case is that upon registration, the user mistypes their mail address and therefore isn’t able to activate their account. On the other hand the invitation code is already disabled so the user hasn’t a chance to do the registration again.

    Any idea how to achieve this? Are there any hooks I can use?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor genetech

    (@genetech)

    Hi,

    The invitation code is saved as used on successful registration. There is no option available to change the invitation code status once the user is verified. It’s not possible because the invitation code is saved in user meta (as user’s data) even if they are not verified.

    Pie Register has an “Invite Through Email” feature in the Premium version. From which admin can add email address/es to invite the users manually. Users can not register if they use a different email address. Learn more here.

    Thanks

    Thread Starter Michael

    (@mischnu)

    Thanks for your reply! I’m using the premium version, but iviting through email isn’t an option in my case: My customer wants/needs to hand out invitation codes to yet unknown users in “real life” so they can come back to the website and register.

    Does Pie Register offer any hooks? Couldn’t find anything about that in the documentation, but it seems there are some?

    Plugin Contributor genetech

    (@genetech)

    Hi,

    There is no hook available from which your requirement makes possible. Because as we mentioned earlier the invitation code is saved in user meta (as user’s data) even if they are not verified. So, if a one-time invitation code is used on one registration then the user needs to use another invitation code unless the code has a usage capability of more than one.

    Thanks

    Thread Starter Michael

    (@mischnu)

    Thanks again. Well, my solution for this would be: On activation of a new user account, search for other users that have the same activation key in their user_meta using a wpdb query:

    SELECT user_id FROM wp_usermeta WHERE meta_key LIKE 'invite_code' AND meta_value LIKE 'ZBX3-8IK9'

    If such users are found, those users should all be inactive, but one could check this additionally to be sure. Next delete them (or disable or whatever) and in any case *now* disable the invitation code.

    So the fact that the key is stored in the user_meta wouldn’t be a real trouble if I ounly could hook into the activation process.

    • This reply was modified 3 years, 4 months ago by Michael.
    Plugin Contributor genetech

    (@genetech)

    Hi,

    You can try use ‘pie_register_after_register’ and ”pie_register_after_register’ hooks for your required customization.

    “pie_register_after_register” action is called when users get registered.
    “pireg_after_verification_users” is called when users get verified.

    Hope it helps.

    If you have the premium version of Pie Register, please log in to your account and submit a support ticket.

    Thanks

    Thread Starter Michael

    (@mischnu)

    Thank you so much, the pireg_after_verification_users hook did do the trick!

    In case anyone else could use it, here’s my aproach:

    add_action( 'pireg_after_verification_users', 'zbx_verify_user' );
    function zbx_verify_user( $user ) {
      $user_id = $user->ID;
      $invite_code = get_user_meta( $user_id, 'invite_code', true );
    
      if ( $invite_code && $invite_code != '' ) {
        // Get all users registered with this invite code
        global $wpdb;
        $user_ids = $wpdb->get_results( "SELECT user_id FROM {$wpdb->prefix}usermeta WHERE meta_key LIKE 'invite_code' AND meta_value LIKE '$invite_code'", ARRAY_A );
    
        // Sort out current user and clean up array
        $other_user_ids = array();
        foreach ( $user_ids as $val ) {
          if ( $val['user_id'] != $user_id )
            $other_user_ids[] = $val['user_id'];
        }
    
        // If any found, DELETE(!) other users
        if ( !empty( $other_user_ids ) ) {
          foreach ( $other_user_ids as $val ) {
            wp_delete_user( $val );
          }
        }
    
        // Deactivate invite code
        $wpdb->update(
          $wpdb->prefix.'pieregister_code',
          array( 'status' => 0, ),
          array( 'name' => $invite_code ),
          array( '%d' ),
          array( '%s' )
        );
      }
    }
    

    Careful, I assume that there aren’t any legit users with the same invite code, so I delete all other users. Keep this in mind when using this code.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Invitation codes vs. email validation’ is closed to new replies.