WooCommerce Email Verification WordPress Code

Ok, i though this will be available since Woocommerce is pretty popular but HOW COME NO ONE ACTUALLY MAKE THIS CODE AND OPEN SOURCE IT?! Holy cow! I search it everywhere and couldn't find WooCommerce verifying user email before allowing the user to access WordPress without verifying whether their email is authentic. In the end i wrote it myself and i'm gonna throw it out for people to use.

Basically the code has to be placed within function.php in your theme, you can also make this into a WordPress plugin and fine tune it if you like. However, please share the code out cause mine isn't the most perfect out there. I am just gonna throw it out and see what other improvement can others suggest or create. Cheers!

// this is just to prevent the user log in automatically after register
function wc_registration_redirect( $redirect_to ) {
        wp_logout();
        wp_redirect( '/sign-in/?q=');
        exit;
}
// when user login, we will check whether this guy email is verify
function wp_authenticate_user( $userdata ) {
        $isActivated = get_user_meta($userdata->ID, 'is_activated', true);
        if ( !$isActivated ) {
                $userdata = new WP_Error(
                                'inkfool_confirmation_error',
                                __( '<strong>ERROR:</strong> Your account has to be activated before you can login. You can resend by clicking <a href="/sign-in/?u='.$userdata->ID.'">here</a>', 'inkfool' )
                                );
        }
        return $userdata;
}
// when a user register we need to send them an email to verify their account
function my_user_register($user_id) {
        // get user data
        $user_info = get_userdata($user_id);
        // create md5 code to verify later
        $code = md5(time());
        // make it into a code to send it to user via email
        $string = array('id'=>$user_id, 'code'=>$code);
        // create the activation code and activation status
        update_user_meta($user_id, 'is_activated', 0);
        update_user_meta($user_id, 'activationcode', $code);
        // create the url
        $url = get_site_url(). '/sign-in/?p=' .base64_encode( serialize($string));
        // basically we will edit here to make this nicer
        $html = 'Please click the following links <br/><br/> <a href="'.$url.'">'.$url.'</a>';
        // send an email out to user
        wc_mail($user_info->user_email, __('Please activate your account'), $html);
}
// we need this to handle all the getty hacks i made
function my_init(){
        // check whether we get the activation message
        if(isset($_GET['p'])){
                $data = unserialize(base64_decode($_GET['p']));
                $code = get_user_meta($data['id'], 'activationcode', true);
                // check whether the code given is the same as ours
                if($code == $data['code']){
                        // update the db on the activation process
                        update_user_meta($data['id'], 'is_activated', 1);
                        wc_add_notice( __( '<strong>Success:</strong> Your account has been activated! ', 'inkfool' )  );
                }else{
                        wc_add_notice( __( '<strong>Error:</strong> Activation fails, please contact our administrator. ', 'inkfool' )  );
                }
        }
        if(isset($_GET['q'])){
                wc_add_notice( __( '<strong>Error:</strong> Your account has to be activated before you can login. Please check your email.', 'inkfool' ) );
        }
        if(isset($_GET['u'])){
                my_user_register($_GET['u']);
                wc_add_notice( __( '<strong>Succes:</strong> Your activation email has been resend. Please check your email.', 'inkfool' ) );
        }
}
// hooks handler
add_action( 'init', 'my_init' );
add_filter('woocommerce_registration_redirect', 'wc_registration_redirect');
add_filter('wp_authenticate_user', 'wp_authenticate_user',10,2);
add_action('user_register', 'my_user_register',10,2);

Ok, i have tested this code with the latest Woocommerce and WordPress 4.2.2. Now, just modify this to suit your needs. It's not perfect yet but i'll leave the perfect version for you guys. Enjoy!

11 thoughts on “WooCommerce Email Verification WordPress Code

  1. Hi Clay,

    That's work perfectly! That's what I'm looking for.

    Just one question, is that possible to redirect new user to their my account page without enter the login details once the user click on the activation link. (something like auto login after activation)

    please help

  2. yeah its possible, you'll just need to alter the verification section and add the following,

    wp_set_current_user ( $userID );
    wp_set_auth_cookie ( $userID );
    $url = "website account page";
    header($url);

    where $userId is the one you want it to be logged in and $url is your website account page

  3. This works well. First good opensource solution for a product that is so wildly used.

    The code sends the "Resend" email with activation link twice. Is there a way to fix this?

    Thanks,
    John

  4. disable the email send from this script and go to your WordPress theme and find the folder (or create a folder and file)

    yourthemename/woocommerce/emails/customer-new-account.php

    and copy the logic of the activation into the above path and attached the link on this php script. When a user register, it will just send from the above template rather than sending the activation and new user registration email separately.

  5. Does it mean that I have to move my_user_register function completely in the new location and then use include to call that file?

    Just so that I'm clear, when the new user registers the first time the code only sends 1 email. It works fine. However if the users tries to login without activating, they get the message for "resend activation email". Upon clicking this the code sends 2 identical emails with same hash coded activation link in each email. The Two emails are sent at the same time.

    Thanks

  6. hmmm..it didn't happen on my Woocommerce installation though. For mine, it sends 2 email too, but one email from this article code and another email from the default new customer registration email. And since this is a problem, the emailing logic of the code provided here needs to move to the path i have just mention in my previous email and not moving the my_user_register function completely to the new location though.

    As for your problem, it seems like a complete different problem entirely, might be some plugin or logic you have added previous?

  7. Really thanks! It's work for me!
    And I recommend if you want a notice after registration
    wp_redirect( get_site_url(). '/my-account/?nt=yes');
    And in function my_init() add
    if(isset($_GET['nt'])){
    wc_add_notice( __( 'Success: Thank you for your registration. Please check your email to activate account! ', 'inkfool' ) );
    }

  8. Great! Worked! But I receive to confirmation on the my account page after successful activation... I received this 2x "Success: Congratulations! Your account has been activated!"

    How can I correct this?

    Cheers!

  9. Hmmmm... when I tried to refresh, eventhough I am already logged in, I still received this notice: "Error: Your account has to be activated before you can login. Please check your email." I am already inside my account but this notice still showed up after I refreshed the browser...

    How can we correct this? That there should no longer be any notices onced logged in...

    Thanks!

  10. thanks guys, i found this very helpful, but i have issues, firstly:
    1. the validation hooks the already existing members, is there a way i can make the existing members login without having to validate?
    2. what do i do to enable auto login as the new user clicks the validation link.
    i would really appreciate guys..

  11. Hi! I pasted the code in the functions.php but I have a few problems. If I try to register as a user on my shop I do not receive the email with the activation code, nor the email that informs me that I have registered on the site. If I try to log in with that e-mail address, I get the message that my email is not activated. Any thoughts on why this is happening?

  12. Just un update... I have received the two emails that you have said in the comments... in my spam folder and after 2 hours. Do you happen to know why this has happened?

Comments are closed.