3

I would like to send an email to all users immediately after they signup that requires them to click a link inside that email to "activate" their email address.

Ideally each user account would have a status showing whether this had been completed or not.

Can anyone recommend a way for me to accomplish this?

0

1 Answer 1

7

For activation process you can follow following steps:

  1. As you can see user_activation_key column in wp_users table. You can make use of that column for sending user activation link.
  2. While signing up users you can insert certain code into that column with custom sql. After the user is signed up with wp_insert_user() and returns an id to $user_id, a custom insert sql can be run

        $user_id = wp_insert_user($user_detail);
    
        if ( $user_id && !is_wp_error( $user_id ) ) {
    
            $code = sha1( $user_id . time() );    
            global $wpdb;    
            $wpdb->update( 
                'wp_users', //table name     
                    array( 'user_activation_key' => $code,  // string    ),       
                    array( 'ID' =>    $user_id ),     
                    array( '%s',    // value1     )
                );
    
            $activation_link = add_query_arg( array( 'key' => $code, 'user' => $user_id ), get_permalink( /* your activation page id here*/ ));  
    
            wp_mail( $user_email, 'SUBJECT', 'Activation link : ' . $activation_link );
        }. 
    

    This will send user an activation link like http://example.com/activation-page/?key=CERTAIN_KEY&user=USER_ID to their email and on the activation page you can run code for activating the users by altering another column user_status from 0 to 1.

  3. After this you can run for another update for user_status column by

    global $wpdb;   
    
    $wpdb->update( 
        'wp_users', //table name     
         array( 'user_status' => 1, // integer   ),       
         array( 'ID' =>    $_GET['user'], //where clause
           'user_activation_key'=>$_GET['key'] //where clause
         ), 
         array( '%d', //value1    )
    );
    

Than you can allow users having status 1 to sign in.

1
  • Beautiful. Just what I was looking for! Thank you.
    – Josh
    Commented May 3, 2017 at 12:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.