Search code examples
phpwordpresswordpress-shortcode

Wordpress incorret password for username or email, added using "wp_insert_user"


So i have created my own user, using below code. I am more than certain that i have a correct password when i login. But for some reason that I dont know i always have "The password you entered for the email address/username is incorrect". Can someone shed some light on me!!!

 function insert_into_wp_users(){
    // Check for nonce security      
    if (!wp_verify_nonce( $_POST['nonce'], 'active-aupair-nonce-string' ) ) {
        die();
    }
    $user_id = wp_insert_user( array(
       'user_email'   => $_POST['email'],
       'user_password'=> $_POST['password'],
    //    'first_name'   => $_POST['firstName'],
    //    'last_name'    => $_POST['lastName'],
       'user_login'   => $_POST['userLogin'],
       'role'         => 'subscriber'
     ));
     if( is_wp_error( $user_id  ) ) {
        wp_send_json_error($user_id->get_error_message());
    } else {
        wp_send_json_success($user_id);
    }
    die();
}

P.S I confirm that my user exist. Wordpress just cant validate the password.


Solution

  • EDIT

    You're using user_password instead of user_pass.

    I lost an hour using your parameters.


    Additionally, it seems that you should also be able to use wp_create_user() as you're not doing anything fancy.

    wp_create_user()

    Creates a new user with just the username, password, and email. For more complex user creation use wp_insert_user() to specify more information.

    add_action( 'init', 'wpso66859906' );
    if ( ! function_exists( 'wpso66859906' ) ) {
        function wpso66859906() {
    
            $user = 'thomas';
            $email = '[email protected]';
            $password = 'password';
    
            if ( ! username_exists( sanitize_user( $user ) ) && is_email( sanitize_email( $email ) ) && ! email_exists( sanitize_email( $email ) ) ) {
                
                wp_create_user( sanitize_user( $user ), trim( $password ), sanitize_email( $email ) );
                
            };
        };
    };