wp_create_user( string $username, string $password, string $email =  ): int|WP_Error

Provides a simpler way of inserting a user into the database.

Description

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

See also

Parameters

$usernamestringrequired
The user’s username.
$passwordstringrequired
The user’s password.
$emailstringoptional
The user’s email.

Default:''

Return

int|WP_Error The newly created user’s ID or a WP_Error object if the user could not be created.

Source

function wp_create_user( $username, $password, $email = '' ) {
	$user_login = wp_slash( $username );
	$user_email = wp_slash( $email );
	$user_pass  = $password;

	$userdata = compact( 'user_login', 'user_email', 'user_pass' );
	return wp_insert_user( $userdata );
}

Changelog

VersionDescription
2.0.0Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Basic Example

    As used in wp-admin/upgrade-functions.php:

    $user_id = username_exists( $user_name );
    
    if ( ! $user_id && false == email_exists( $user_email ) ) {
    	$random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false );
    	$user_id = wp_create_user( $user_name, $random_password, $user_email );
    } else {
    	$random_password = __( 'User already exists.  Password inherited.', 'textdomain' );
    }

You must log in before being able to contribute a note or feedback.