Adding and updating users in WordPress through the program

We can use the wp_create_user() maybe wp_insert_user() function to add users in WordPress, in this article, we will show how to add and update users in WordPress using these two functions with a few examples, explaining the similarities and differences between them.

Differences between wp_create_user and wp_insert_user

wp_create_user

This function allows us to add a user by providing only the three required pieces of information: username, password, and email. The user role is the default role set by the website for new users. In factwp_create_user work in wp_insert_user And above that, yes. wp_insert_user The simplified packaging of the

wp_insert_user

This function is the complete way to add a WordPress user programmatically, and we can pass in some more detailed information about the user, such as last name, display name, user role, and so on.

typical example

We know the basic information of these two functions, let's look at the sample code.

Simple Example of Creating WordPress Users Programmatically

Now, let's create a subscriber user with the name peter and the password 123456 (never use this password in a real project, read how to use the wp_genetate_password() function to generate the password), we will use these two functions separately to accomplish this so you can understand the differences more clearly.

Creating a user with wp_create_user

$user_id = wp_create_user( 'peter', '123456' );

if ( is_wp_error( $user_id ) ) {
// If for some reason the user could not be created, display an error message
echo $user_id->get_error_message();
} else {
// When it's done, display a successful creation message
echo 'User created successfully!' ;
}

Creating a user with wp_insert_user

$user_id = wp_insert_user(
// Provide all the data here as an array
[
'user_login' => 'peter',
'user_pass' => '1234546',
]
).

if ( is_wp_error( $user_id ) ) {
// If for some reason the user cannot be created, display an error message
echo $user_id->get_error_message();
} else {
// When it's done, display a successful creation message
echo 'User created successfully!' ;
}

Once created, the newly added user will be displayed in the user list, which is the same as adding users directly in the backend.

Adding administrator users through the program

If we need to specify the role of a user when adding a user, such as adding an administrator user, we can no longer use the wp_create_user() function, as it is not possible to specify a user role. In this case, we need to use the wp_insert_user() function to add users.

$admin_user_id = wp_insert_user(
[
'user_login' => 'peter',
'user_pass' => '123456',
'role' => 'administrator',
]
).

if ( ! is_wp_error( $admin_user_id ) ) {
echo 'Administrator user created successfully! ;
}

Complete data that can be provided when creating a user

We know that WordPress users' basic information is stored in the wp_users datasheetAdditional information is stored in the wp_usermeta table, where information stored in the wp_users table can be passed directly to the wp_insert_user() function to add, stored in the wp_usermeta datasheet, thepart ofAdditional information can also be passed when creating a user to the wp_insert_user() function, more additional information can be found after creating the function using the update_user_meta() to add.

See the following example

$userdata = [
'user_login' => 'peter',


'user_email' => 'peter@example.com',



'display_name' => 'Peter Liu', 'user_url' =>
'user_url' => 'https://wpzhiku.com.com', 'description' => 'A couple
'description' => 'A couple words about Peter here.



'admin_color' => 'fresh',
'use_ssl' => false, 'user_registered' => false, 'admin_color' => 'fresh', 'use_ssl' => false, 'user_registered' => false
'user_registered' => '2023-12-31 00:00:00',
'show_admin_bar_front' => 'true',
'role' => 'subscriber', 'locale' => 'subscriber', 'user_registered' => false
'locale' => '', .
].

$user_id = wp_insert_user( $userdata );

if ( ! is_wp_error( $user_id ) ) {
update_user_meta( $user_id, 'aim', '' );
update_user_meta( $user_id, 'yim', '' );
update_user_meta( $user_id, 'jabber', '' );
}

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *