WooCommerce minimum password length requirement

How to set a minimum password length for WooCommerce account registration, password update, and password reset fields. It also includes maximum password length!

The following code, by default, is set to only require a minimum password length, which is currently set to be 15 characters. If you wish to also enable a maximum password length on the WooCommerce registration, password update, or password reset fields, then you’ll need to uncomment the applicable lines in the following code. Remember to change the character values to suit your needs.

add_action('woocommerce_process_registration_errors', 'validatePasswordReg', 10, 2 );

function validatePasswordReg( $errors, $user ) {
	// change value here to set minimum required password chars
    if(strlen($_POST['password']) < 15  ) {
    	$errors->add( 'woocommerce_password_error', __( 'Password must be at least 15 characters long.' ) );
    }
    // adding ability to set maximum allowed password chars — uncomment the following two (2) lines to enable that
    //elseif (strlen($_POST['password']) > 16 )
     	//$errors->add( 'woocommerce_password_error', __( 'Password must be shorter than 16 characters.' ) );
    return $errors;
	}

add_action('woocommerce_save_account_details_errors', 'validateProfileUpdate', 10, 2 );

function validateProfileUpdate( $errors, $user ) {
	// change value here to set minimum required password chars
    if(strlen($_POST['password_2']) < 15  ) {
    	$errors->add( 'woocommerce_password_error', __( 'Password must be at least 15 characters long.' ) );
	}
	// adding ability to set maximum allowed password chars — uncomment the following two (2) lines to enable that
    //elseif (strlen($_POST['password_2']) > 16 )
     	//$errors->add( 'woocommerce_password_error', __( 'Password must be shorter than 16 characters.' ) );
    return $errors;
	}

add_action('woocommerce_password_reset', 'validatePasswordReset', 10, 2 );

function validatePasswordReset( $errors, $user ) {
	// change value here to set minimum required password chars — uncomment the following two (2) lines to enable that
    if(strlen($_POST['password_3']) < 15  ) {
    	$errors->add( 'woocommerce_password_error', __( 'Password must be at least 15 characters long.' ) );
	}
	// adding ability to set maximum allowed password chars — uncomment the following two (2) lines to enable that
    //elseif (strlen($_POST['password_3']) > 16 )
     	//$errors->add( 'woocommerce_password_error', __( 'Password must be shorter than 16 characters.' ) );
    return $errors;
	}


add_action( 'woocommerce_after_checkout_validation', 'minPassCharsCheckout', 10, 2 );
function minPassCharsCheckout( $user ) {
	// change value here to set minimum required password chars on checkout page account registration
    if ( strlen( $_POST['account_password'] ) < 15  ) {
    	wc_add_notice( __( 'Password must be at least 15 characters long.', 'woocommerce' ), 'error' );
	}
}


Was this helpful? Please buy me a coffee






This code should be added to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update.

Share your thoughts

Your email address will not be published. Required fields are marked *