Preventing Direct Access: Implementing Email Confirmation for New WooCommerce User Registrations

fajibom662

New member
XNullUser
Joined
Mar 17, 2024
Messages
3
Reaction score
0
Points
1
Location
spain
NullCash
19
To prevent users from being logged in directly upon registration in WooCommerce and instead require them to confirm their email first, you can use a plugin or custom code solution. One way to achieve this is by using a plugin like "WooCommerce Email Verification" or by implementing custom code. Here's how you can do it with custom code:

  1. Disable auto-login after registration: You need to disable the automatic login that WooCommerce performs after user registration.
  2. Send a confirmation email: You should send a confirmation email to the user upon registration with a unique verification link.
  3. Verify email upon confirmation: When the user clicks on the verification link, you need to verify their email address before allowing them to log in.
Here's a basic outline of how you can implement this using custom code:
PHP:
// Disable auto-login after registration
function disable_auto_login() {
    remove_action( 'woocommerce_registration_redirect', 'wc_auto_login_after_registration', 10 );
}
add_action( 'init', 'disable_auto_login' );

// Send confirmation email with verification link
function send_verification_email( $user_id ) {
    $user = get_userdata( $user_id );
    $verification_key = md5( $user->user_email . time() );
    update_user_meta( $user_id, 'verification_key', $verification_key );

    $subject = 'Please confirm your email address';
    $message = 'Click the following link to confirm your email address: ' . site_url() . '/verify-email/?key=' . $verification_key;

    wp_mail( $user->user_email, $subject, $message );
}
add_action( 'woocommerce_created_customer', 'send_verification_email' );

// Verify email upon confirmation
function verify_email() {
    if ( isset( $_GET['key'] ) && isset( $_GET['email'] ) ) {
        $user = get_user_by( 'email', $_GET['email'] );
        $verification_key = get_user_meta( $user->ID, 'verification_key', true );

        if ( $verification_key === $_GET['key'] ) {
            // Email is verified, log the user in
            wp_set_auth_cookie( $user->ID );
            wp_redirect( home_url() ); // Redirect to home page after login
            exit;
        } else {
            // Invalid verification key
            wp_redirect( home_url( '?email_verification_error=true' ) ); // Redirect with error message
            exit;
        }
    }
}
add_action( 'init', 'verify_email' );
This code should be added to your theme's functions.php file or in a custom plugin. It disables auto-login after registration, sends a confirmation email upon registration, and verifies the email when the user clicks on the verification link. Make sure to customize the email content, subject, and any URLs as per your requirements. Additionally, you may need to adjust the code to fit your specific setup and requirements.
 

prexious

New member
XNullUser
Joined
Aug 18, 2024
Messages
3
Reaction score
0
Points
1
Location
AFghni
NullCash
7
To prevent users from being logged in directly upon registration in WooCommerce and instead require them to confirm their email first, you can use a plugin or custom code solution. One way to achieve this is by using a plugin like "WooCommerce Email Verification" or by implementing custom code. Here's how you can do it with custom code:

  1. Disable auto-login after registration: You need to disable the automatic login that WooCommerce performs after user registration.
  2. Send a confirmation email: You should send a confirmation email to the user upon registration with a unique verification link.
  3. Verify email upon confirmation: When the user clicks on the verification link, you need to verify their email address before allowing them to log in.
Here's a basic outline of how you can implement this using custom code:
PHP:
// Disable auto-login after registration
function disable_auto_login() {
    remove_action( 'woocommerce_registration_redirect', 'wc_auto_login_after_registration', 10 );
}
add_action( 'init', 'disable_auto_login' );

// Send confirmation email with verification link
function send_verification_email( $user_id ) {
    $user = get_userdata( $user_id );
    $verification_key = md5( $user->user_email . time() );
    update_user_meta( $user_id, 'verification_key', $verification_key );

    $subject = 'Please confirm your email address';
    $message = 'Click the following link to confirm your email address: ' . site_url() . '/verify-email/?key=' . $verification_key;

    wp_mail( $user->user_email, $subject, $message );
}
add_action( 'woocommerce_created_customer', 'send_verification_email' );

// Verify email upon confirmation
function verify_email() {
    if ( isset( $_GET['key'] ) && isset( $_GET['email'] ) ) {
        $user = get_user_by( 'email', $_GET['email'] );
        $verification_key = get_user_meta( $user->ID, 'verification_key', true );

        if ( $verification_key === $_GET['key'] ) {
            // Email is verified, log the user in
            wp_set_auth_cookie( $user->ID );
            wp_redirect( home_url() ); // Redirect to home page after login
            exit;
        } else {
            // Invalid verification key
            wp_redirect( home_url( '?email_verification_error=true' ) ); // Redirect with error message
            exit;
        }
    }
}
add_action( 'init', 'verify_email' );
This code should be added to your theme's functions.php file or in a custom plugin. It disables auto-login after registration, sends a confirmation email upon registration, and verifies the email when the user clicks on the verification link. Make sure to customize the email content, subject, and any URLs as per your requirements. Additionally, you may need to adjust the code to fit your specific setup and requirements.
This is a lifesaver. my new users were always being logged in automatically upon registration. thankl you for the update
 

res7art

Member
XNullUser
Joined
Feb 24, 2022
Messages
181
Reaction score
0
Points
16
NullCash
7
Actually users can stil log in even without confirming the email link. The code needs something more to disable the log in without confirming the link
 

hdvagal

New member
XNullUser
Joined
Sep 6, 2024
Messages
8
Reaction score
0
Points
1
Location
india
NullCash
5
Can anyone confirm if this code still works or need any improvement
 
Top