I have the following code:
function create_account(){
//You may need some data validation here
$fname = ( isset($_POST['billing_first_name']) ? $_POST['billing_first_name'] : '' );
$lname = ( isset($_POST['billing_last_name']) ? $_POST['billing_last_name'] : '' );
$email = ( isset($_POST['email']) ? $_POST['email'] : '' );
$pass = ( isset($_POST['password']) ? $_POST['password'] : '' );
$phone = ( isset($_POST['billing_phone']) ? $_POST['billing_phone'] : '' );
if ( !fname_exists( $fname ) && !lname_exist( $lname ) && !email_exists( $email ) && !pass_exist( $pass ) && !phone_exist( $phone ) ) {
$user_id = wp_create_user( $fname, $lname, $email, $pass, $phone );
if( !is_wp_error($user_id) ) {
//user has been created
$user = new WP_User( $user_id );
$user->set_role( 'customer' );
//Redirect
wp_set_auth_cookie( $user_id, false, is_ssl() );
wp_redirect( '/checkout' );
exit;
} else {
//echo 'Register failed';
}
}
}
add_action('init','create_account');
This is working properly except login. I can't log in. The password is wrong, but after the password is changed in the database or wp-admin, then login is working properly. So I am sure my code is not storing the password in the database.
Here is my form:
<form method="post" action="" class="pwsuf-user-form pwsuf-signup-form">
<div class="pwsuf-result-error"></div>
<div class="pwsuf-form-row form-row validate-required">
<input type="text" name="billing_first_name" id="billing_first_name" class="pwsuf-input input-text" placeholder="First name" value="" required=""><span class="pwsuf-error">Please enter First name</span></div>
<div class="pwsuf-form-row form-row validate-required">
<input type="text" name="billing_last_name" id="billing_last_name" class="pwsuf-input input-text" placeholder="Last name" value="" required=""><span class="pwsuf-error">Please enter Last name</span></div>
<div class="pwsuf-form-row form-row validate-required">
<input type="email" name="email" id="email" class="pwsuf-input input-text" placeholder="Email" value="" required=""><span class="pwsuf-error">Please enter Email</span></div>
<div class="pwsuf-form-row form-row ">
<input type="password" name="password" id="password" class="pwsuf-input input-text" placeholder="Password" value="" required=""><span class="pwsuf-error">Please enter Password</span></div>
<div class="pwsuf-form-row form-row validate-required">
<input type="tel" name="billing_phone" id="billing_phone" class="pwsuf-input input-text" placeholder="Phone" value="" required=""><span class="pwsuf-error">Please enter Phone</span></div>
<div class="pwsuf-form-row pwsuf-row-btn checkout-buttons-wrap">
<span data-link="step-2" data-page="step-1" class="button alt wc-forward fleft no-validate">Continue as Guest</span>
<input type="hidden" name="wp-submit-type" value="register">
<input type="submit" name="signup-submit" class="pwsuf-button pwsuf-singup btn checkout-button" value="Create account">
<div class="loader-fountain"></div>
</div>
<div class="checkout-buttons-wrap">
<span class="pwsuf-link pwsuf-link_reg">Already have account</span>
</div>
</form>
wp_create_user
does not accept a first and last name as the first and second parameters