These two functions are available to create a new user in wordpress.
These both functions does the exact thing except wp_create_user is wrapper as in more simplified version of wp_insert_user.
wp_create_user( string $username, string $password, string $email = ” )
wp_create_user() function creates a new user with just the username, password, and email, for more complex user creation or to specify more information about user we use wp_insert_user() function.
wp_insert_user( array|object|WP_User $userdata )
As you can see wp_create_user() function accept three parameters username , password and email of the new user, wp_inser_user accept array/object of new user.
Let’s see how these functions work :
When creating a new user using wp_create_user() function, we need to pass three parameters :
function create_user( $user_name, $user_email ) {
$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.', 'textdomain' );
}
}
For the sake of simplicity, we copied this code from the official document of wordpress. I just wrapped this code with in a function named create_user.
As you can see , there is two parameters only passed to the function. Why ? We’ll get there soon. Line of code, we checked does the $user_name and $user_email exists in our database , if false than we generated random password with a length of 12 and excluding standard special characters in our password like double quote (“), Number sign (hash),Dollar sign ($) . If $user_name and $user_email already exist in our database , Message will be shown “User already exists. Password inherited”.
After generating the $random_password, now we have three parameters with us . wp_create_user( $user_name, $random_password, $user_email )
After passing the parameters, the function will create a new users. If user is not created due to problems , WP_Error come’s in handy . we’ll talk about WP_Error in up coming days.
As WordPress documentation says ” wp_insert_user() is a more complete way to create a new user.”
Absolutely with above statement, cause wp_create_user() function uses the wp_insert_user() function under the hood. Let’s look at the wp_create_user() function :
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
);
}
We can see that the parameters that we passed will be checked using wp_slash() function, which add slashes to a string or array of strings and then all the parameters are passed down to the compact function.
compact( ) function in php creates an array from variables and their values.
You see the array contains our username, email and password and finally passed down to the wp_insert_user() function.
let’s dive into wp_insert_user() function for a bit. As you can see wp_insert_user() function accepts (array|object|WP_User) $userdata, $userdata may contain ID, user_pass, user_login, user_nicename, user_email and many other attributes for the user. You can see all the attributes from this link .
The wp_insert_user() is a complete function for creating a user so it contains little more lines of codes compared to the wp_create_user() function.
https://developer.wordpress.org/reference/functions/wp_insert_user/#source
In above link , you can see the whole function for wp_insert_user() . In the function , $wpdb is called which is a class that contains a set of functions used to interact with a database. After checking if the $userdata is instanceof stdClass or WP_User, The function will check if its about to create new instance of user or the update of the user.
if
( !
empty
(
$userdata
[
'ID'
] ) ) {}
After that , you can see there is whole lot of checking if username exists, email exists , generating hash password for the user. This is how wp_insert_user() functions works in a brief introduction.