WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp create user

Description

The wp_create_user function allows you to insert a new user into the WordPress database. It uses the $wpdb class to escape the variable values, preparing it for insertion into the database. Then the PHP compact() function is used to create an array with these values. To create a user with additional parameters, use wp_insert_user().

Usage

 <?php wp_create_user$username$password$email ); ?> 

Example

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

$user_id = username_exists( $user_name );
if ( !$user_id and email_exists($user_email) == false ) {
	$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.');
}

Parameters

$username
(string) (required) The username of the user to be created.
Default: None
$password
(string) (required) The password of the user to be created.
Default: None
$email
(string) (optional) The email address of the user to be created.
Default: None

Returns

When successful - this function returns the user ID of the created user. In case of failure (username or email already exists) the function returns an error object, with these possible values and messages;

  • empty_user_login, Cannot create a user with an empty login name.
  • existing_user_login, This username is already registered.
  • existing_user_email, This email address is already registered.

Source File

wp_create_user() is located in wp-includes/user.php.

See also index of Function Reference and index of Template Tags.