wp login form - Set user password after creating user

I have the following code:function create_account(){You may need some data validation here$fname = ( isset($_POST[

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>

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>
Share Improve this question edited Dec 10, 2019 at 14:54 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Dec 10, 2019 at 13:33 LerryLerry 1111 gold badge2 silver badges10 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744921120a4601137.html

相关推荐

  • wp login form - Set user password after creating user

    I have the following code:function create_account(){You may need some data validation here$fname = ( isset($_POST[

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信