简体   繁体   中英

Add User in wordpress without username

I have made a registration form which have only two fields email and password .Now i want to add user to Wordpress database but wp_create_user() require username too.Then i tried using another function as $status=wp_insert_user($userdata); where $userdata is an array containing elements as

$userdata = array(
                'user_pass'   =>  $pswrd,
                'user_email'=> $email
            );

but user is not added in database. Is their any other function through which i can add user with above two fields only.

Try this one example

$userdata = array(
                    'user_login'  =>  '',
                    'user_pass'   =>  $pswrd,
                    'user_email'=> $email
                );

If you don't want to ask the user for a specific login name, you could use their e-mail:

if ( !username_exists( $email ) {
    $user_id = wp_create_user( $email, $pswrd, $email );

    if ( !is_wp_error( $user_id ) ) {
        // created succesfully

    }
} else {
    // Username already exists

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM