创建用户后设置用户密码

时间:2019-12-10 作者:Lerry

我有以下代码:

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\');
除登录外,其他操作正常。我无法登录。密码错误,但在数据库或wp admin中更改密码后,登录即可正常工作。因此,我确信我的代码没有将密码存储在数据库中。

这是我的表格:

<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>

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

卡佩托是对的,你的电话是这样的:

$user_id = wp_create_user( $fname, $lname, $email, $pass, $phone );
但这些不是正确的参数wp_create_user:

wp_create_user( $username, $password, $email );
您要创建的是一个用户名为名字、密码为姓氏的用户。

相关推荐