wp_create_user()
A simpler way of inserting a user into the database.
Creates a new user with just the username, password, and email. For more complex user creation use wp_insert_user() to specify more information (the function is also based on this function).
Uses: wp_insert_user()
Used By: register_new_user()
No Hooks.
Returns
Int|WP_Error. The newly created user's ID or a WP_Error object if the user could not be created.
Usage
wp_create_user( $username, $password, $email );
- $username(string) (required)
- The user's username.
- $password(string) (required)
The user's password.
It has the attribute #[\SensitiveParameter], which hides the value of the parameter from logs. It is used to protect sensitive data (for example, passwords). Documentation.
- $email(string)
- The user's email.
Default:''
Examples
#1 Creating a new user
If WordPress can't create a user, it will return WP_Error object — so let's output the error message in this case:
$random_password = wp_generate_password( 12 );
$user_id = wp_create_user( $user_name, $random_password, $user_email );
if ( is_wp_error( $user_id ) ) {
echo $user_id->get_error_message();
}
else {
echo 'User has been created.';
}
Notes
- See: wp_insert_user() More complete way to create a new user.
Changelog
| Since 2.0.0 | Introduced. |
wp_create_user() wp create user code WP 7.0
function wp_create_user(
$username,
#[\SensitiveParameter]
$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 );
}