Let New Users Choose Their Password When Registering
Adds password and confirm-password fields to the WordPress registration form, so new members pick their own password instead of waiting for a "set your password" email.
Opens WPIDE on
Code
// Minimum password length.
$wpide_reg_min_length = 10;
// Log the new member in right after registering.
$wpide_reg_auto_login = false;
add_action( 'register_form', function () use ( $wpide_reg_min_length ) {
?>
<p>
<label for="wpide_pass1">Password</label>
<input type="password" name="wpide_pass1" id="wpide_pass1" class="input" autocomplete="new-password" minlength="<?php echo (int) $wpide_reg_min_length; ?>" required>
</p>
<p>
<label for="wpide_pass2">Confirm password</label>
<input type="password" name="wpide_pass2" id="wpide_pass2" class="input" autocomplete="new-password" required>
</p>
<?php
} );
add_filter( 'registration_errors', function ( $errors ) use ( $wpide_reg_min_length ) {
if ( ! isset( $_POST['wpide_pass1'] ) ) {
return $errors;
}
$pass1 = (string) wp_unslash( $_POST['wpide_pass1'] );
$pass2 = isset( $_POST['wpide_pass2'] ) ? (string) wp_unslash( $_POST['wpide_pass2'] ) : '';
if ( strlen( $pass1 ) < $wpide_reg_min_length ) {
$errors->add( 'wpide_pass_short', sprintf( '<strong>Error:</strong> Your password must be at least %d characters long.', $wpide_reg_min_length ) );
} elseif ( $pass1 !== $pass2 ) {
$errors->add( 'wpide_pass_mismatch', '<strong>Error:</strong> The two passwords do not match.' );
}
return $errors;
} );
// register_new_user fires once the account exists and errors have passed.
add_action( 'register_new_user', function ( $user_id ) use ( $wpide_reg_auto_login ) {
if ( empty( $_POST['wpide_pass1'] ) ) {
return;
}
wp_set_password( (string) wp_unslash( $_POST['wpide_pass1'] ), $user_id );
// They know their password, so only tell the admin about the new account.
remove_action( 'register_new_user', 'wp_send_new_user_notifications' );
wp_new_user_notification( $user_id, null, 'admin' );
if ( $wpide_reg_auto_login ) {
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id );
}
}, 5 );
add_filter( 'registration_redirect', function ( $redirect ) use ( $wpide_reg_auto_login ) {
if ( empty( $_POST['wpide_pass1'] ) ) {
return $redirect;
}
return $wpide_reg_auto_login ? home_url( '/' ) : add_query_arg( 'wpide_registered', 1, wp_login_url() );
} );
add_filter( 'login_message', function ( $message ) {
if ( isset( $_GET['wpide_registered'] ) ) {
$message .= '<p class="message">Registration complete. You can log in with your new password now.</p>';
}
return $message;
} );
Notes
Set $wpide_reg_auto_login to true to log new members in straight away. Only the wp-login.php registration form is changed.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.