Working with Users
Topics
Adding Users Adding Users
To add a user you can use wp_create_user() or wp_insert_user().
wp_create_user() creates a user using only the username, password and email parameters while while wp_insert_user() accepts an array or object describing the user and it’s properties.
Create User Create User
1 2 3 4 5 | wp_create_user( string $username , string $password , string $email = '' ); |
Allows you to create a new WordPress user.
Note:
It uses wp_slash() to escape the values. The PHP compact() function to create an array with these values. The wp_insert_user() to perform the insert operation.
Please refer to the Function Reference about wp_create_user() for full explanation about the used parameters.
Example Create Example Create
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // check if the username is taken $user_id = username_exists( $user_name ); // check that the email address does not belong to a registered user if (! $user_id && email_exists( $user_email ) === false) { // create a random password $random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false ); // create the user $user_id = wp_create_user( $user_name , $random_password , $user_email ); } |
Insert User Insert User
1 2 3 | wp_insert_user( array |object|WP_User $userdata ); |
Note:
The function calls a filter for most predefined properties.
The function performs the action user_register
when creating a user (user ID does not exist).
The function performs the action profile_update
when updating the user (user ID exists).
Please refer to the Function Reference about wp_insert_user() for full explanation about the used parameters.
Example Insert Example Insert
Below is an example showing how to insert a new user with the website profile field filled in.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $username = $_POST [ 'username' ]; $password = $_POST [ 'password' ]; $website = $_POST [ 'website' ]; $user_data = [ 'user_login' => $username , 'user_pass' => $password , 'user_url' => $website , ]; $user_id = wp_insert_user( $user_data ); // success if (!is_wp_error( $user_id )) { echo 'User created: ' . $user_id ; } |
Updating Users Updating Users
1 2 3 | wp_update_user( mixed $userdata ); |
Updates a single user in the database. The update data is passed along in the $userdata array/object.
To update a single piece of user meta data, use update_user_meta() instead.
To create a new user, use wp_insert_user() instead.
Note:
If current user’s password is being updated, then the cookies will be cleared!
Please refer to the Function Reference about wp_update_user() for full explanation about the used parameters.
Example Update Example Update
Below is an example showing how to update a user’s website profile field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $user_id = 1; $user_id = wp_update_user( [ 'ID' => $user_id , 'user_url' => $website , ] ); if (is_wp_error( $user_id )) { // error } else { // success } |
Deleting Users Deleting Users
1 2 3 4 | wp_delete_user( int $id , int $reassign = null ); |
Delete the user and optionally reassign associated entities to another user ID.
Note:
The function performs the action deleted_user
after the user have been deleted.
Alert:
If the $reassign parameter is not set to a valid user ID, then all entities belonging to the deleted user will be deleted!
Please refer to the Function Reference about wp_delete_user() for full explanation about the used parameters.