Adding a user in WordPress is easy. WordPress comes with a built-in user management system. This lets you add users with different roles and permission levels. In this article, we will teach how to add a user to WordPress through the dashboard and programmatically and directly in the database.
The first method is for users who have admin access to the WordPress dashboard, and the programming method is for programmers and theme and plugin developers, and the database method is also for those who have access to the WordPress database but do not have access to the dashboard in any way.
What Are User Roles?
User roles allow you to assign different levels of permissions to users on your website. If you don’t want a user to have all the permissions of the administrator role, you can choose from Editor, Author, Contributor, or Subscriber.
- Administrator: somebody who has access to all the administration features within a single site.
- Editor: somebody who can publish and manage posts including the posts of other users.
- Author: somebody who can publish and manage their own posts.
- Contributor: somebody who can write and manage their own posts but cannot publish them.
- Subscriber: somebody who can only manage their profile.
Each user role is capable of everything that a less powerful role is capable of. In other words, Editors can do everything Authors can do, Authors can do everything Contributors can do, and so on.
Some plugins add other roles to this list, each of which has its own use, such as the WooCommerce plugin, which adds the Customer role and is only used for customers.
How To Add A New User via WordPress Dashboard (Manually)
Go to your WordPress admin page and log in to your website Dashboard. You simply need to go to the Users » Add New page in your WordPress admin area.
Next, you just have to fill out the form to create a new user.
Enter a username and email address for your new user. First name, last name, and website are optional.
Enter a SECURE password for your new user. You can either use the one that WordPress randomly generates, or create your own.
Keep the Send User Notification checked if you would like the new user to receive an email with their login information.
Choose a Role for your new user. Administrator is the default and it gives the user access to all the features of the website. If you need to limit the permissions of your new user, see the other roles available below.
Click the Add New User button and you’re all set!
How to create WordPress user programmatically with code
There are many reasons why you might want to create a WordPress user programmatically. For example, if you are developing a plugin or a custom theme that requires users to register, you may want to automatically create a new user when they register.
The wp_create_user() function
The wp_create_user()
is a simple function accepts just 3 arguments – the username (required), password (required), and email (optional) and creates a new WordPress user.
To add a user using a function in WordPress programmatically, just open the functions.php file or your plugin file and put the following code in it.
wp_create_user( string $username, string $password, string $email = '' )
Example
wp_create_user( 'honar', '123456789', 'info@honarsystems.com' );
Or you can even use the functions of WordPress and generate a random password or check the presence or absence of the user.
$user_name = 'honar';
$user_email = 'info@honarsystems.com';
$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 );
if(is_wp_error($result)){
$error = $result->get_error_message();
}else{
$user = get_user_by('id', $result);
}
} else {
$random_password = __( 'User already exists. Password inherited.', 'textdomain' );
}
If the user is successfully created, its ID is returned by the function. Otherwise, an error is returned.
The wp_insert_user() function
The wp_create_user() function is a function that can easily create a user in WordPress, but sometimes we need to control more options, in which case we use the wp_insert_user() function.
wp_insert_user( array|object|WP_User $userdata )
$user_data = array(
'ID' ( null | integer ),
'user_pass' ( null | string ),
'user_login' ( null | string ),
'user_nicename' ( null | string ),
'user_url' ( null | string ),
'user_email' ( null | string ),
'display_name'( null | string ),
'nickname' ( null | string ),
'first_name' ( null | string ),
'last_name' ( null | string ),
'description' ( null | string ),
'user_registered' ( null | string ),
'role' ( null | string ),
'jabber' ( null | string ),
'aim' ( null | string ),
'yim' ( null | string ),
'locale' ( null | string ) ,
//...
);
But the true secret is inside the $user_data argument. This can be either an array, an instance of stdClass, or an instance of WP_User
. These are the possible values for an array of formatted data:
Also, by using this function, users’ information can be updated, because of this, many input data can be NULL.
$user_id = wp_insert_user( array(
'user_login' => 'honar',
'user_pass' => '123456789',
'user_email' => 'info@honarsystems.com',
'first_name' => 'Honar',
'last_name' => 'Systems',
'display_name' => 'Honar Systems',
'role' => 'editor'
));
As you can see, we can control many things in this function, such as the role that we have chosen in this example, editor.
How to create a WordPress user via the database
To create a user in WordPress using the database, you must access the WordPress database through the host (such as CPanel or AirectAdmin).
Log in to your host.
Enter the PHPMyAdmin section of your host and click on the WordPress database you want.
Select wp_users inside the tables. The prefix of your table may not be wp_, in this case, choose the table that ends with users.
Select the Insert option from the menu and fill the data in the fields as below.
- ID: leave it empty
- user_login: honar
- user_pass: This password is MD5. To hash your raw password you can use online websites. Or select MD5 from Function dropdown and write your raw password in the field like ‘123456’. It will hash your password.
- user_nicename: honar
- user_email: info@honarsystems.com
- user_url: https://honarsystems.com/
- user_registered: choose date and time
- user_activation_key: leave it empty
- user_status: 0
- display_name: Honar Systems
Click the Go button.
In this section, the desired user has been added, but there is still one step left.
From the list of tables, select the wp_usermeta table.
Select the Insert option from the menu and fill the fields as below.
- umeta_id: leave it empty
- user_id: This is the user ID that we created in the previous step. Go to the wp_users table and view the user ID from there.
- meta_key:
wp_capabilities
- meta_value: a:1:{s:6:”editor”;b:1;}
In this section, we define the role of the user as editor. If we want to define the manager, just enter the following code in meta_value.
Now, if we go to the Users menu in the WordPress administration, we will see that a new user has been created with the role editor.
Conclusion
In this article, we examined how to create a new user in WordPress using the dashboard (manually), creating through programming (with the wp_create_user() and wp_insert_user() functions) and the database.