1

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>
3
  • 1
    you don't put the good arguments for wp_create_user
    – Kaperto
    Commented Dec 10, 2019 at 13:39
  • wp_generate_password not very good idea, because i need got user password from my form -user determines the password for himself
    – Lerry
    Commented Dec 10, 2019 at 13:44
  • 1
    @Lerry that's not what Kaperto is talking about, you aren't calling the function with the correct parameters, wp_create_user does not accept a first and last name as the first and second parameters
    – Tom J Nowell
    Commented Dec 10, 2019 at 13:47

1 Answer 1

2

Kaperto is right, your call looks like this:

$user_id = wp_create_user( $fname, $lname, $email, $pass, $phone );

But those are not the correct parameters for wp_create_user:

wp_create_user( $username, $password, $email );

What you're creating is a user whose username is the first name, and their password is their last name.

2
  • oh Sir, many thanks, but if i need also $fname, $lname, $phone how better i can use this?
    – Lerry
    Commented Dec 10, 2019 at 13:49
  • That's a very good question, eitherway you need to pass a username as the first parameter, I suggest you manufacture one using the first and last names. If you want to set the first/last name fields of that user though, that's a good question, and one you should ask on the site as a separate question "Given a user ID that has just been created, how do I set the first and last name of that user?". You're much more likely to get a good answer that way than a quick comment can ever manage
    – Tom J Nowell
    Commented Dec 10, 2019 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.