WordPress at a glance

wp_create_user() WP 2.0.0

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).

Is the basis for: register_new_user()
Works based on: wp_insert_user()

No Hooks.

Return

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.
$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.

Code of wp create user: wp-includes/user.php WP 5.2.2

<?php
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 );
}

Related Functions

From tag: Users (_user)

More from category: User insertion/removal

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register