How to create WordPress user programmatically
Are you looking for alternative ways to create users that don’t involve the admin dashboard? If so, this post is for you. In this guide, we’ll show you how to create a WordPress user programmatically along with sample scripts that you can use on your site.
Nowadays, most websites allow visitors to create an account and become registered users to get certain benefits such as exclusive deals and discounts.
When a new user registers, the site gathers information about the profile and assigns a role to that user. These roles allow you to set different permissions for each user, such as accessing some restricted content, displaying a specific navigation menu for each role, having access to certain offers, and so on.
WordPress allows you to manage different types of users and provides full user management features. Using the multiple roles available by default, you can create, update and delete users and assign different permissions to each one. This is good for some websites, but if you want a more complex solution to manage different types of permissions, you might need to create WordPress users programmatically.
Let’s have a look at how you can do that.
How to create a WordPress user programmatically
To create users programmatically, you can take advantage of some of the WordPress inbuilt functions. In all cases, the only required values are name and password.
Once a user is created, WordPress can send an email to complete the registration from their mailbox. Even if the user already knows the login credentials, it’s always a good idea to send user registration emails for a few reasons:
- You make sure that the email they provided is correct and don’t have any typos
- Users can go back to that email in case they forget their user or password
When creating users, you can assign a password, name, and role. Later on, users can change and update any of these details as well as all the information on their profiles.
Now that we have a clear idea of what you can do, let’s have a look at some sample scripts that will allow you to create users in different ways.
NOTE: As we’ll edit some core files, before you start, we recommend you generate a full backup of your site. Additionally, we recommend you create a child theme or use a child theme plugin if you aren’t using one already.
Sample Scripts to Create WordPress Users with Code
In this section, we’ll show you several scripts that you can use to create users on your site programmatically. You’ll find the scripts and an explanation of what each section of the code does.
Create a single user
Creating a WordPress user programmatically is quite simple and only requires a single line of code. The only required values are name and password and you can also set the password using the wp_create_user()
function as follows:
wp_create_user( 'johndoe', 'passwordgoeshere', '[email protected]' );
Paste this line of code on the functions.php
file of your child theme to create a user called johndoe.
On top of that, we can do a few more things to improve the above code. For example, to prevent creating the user when they’re navigating through backend pages or reloading the screen, you should use a hook. The new code will look something like this:
add_action('admin_init','QuadLayers_create_user'); function QuadLayers_create_user(){ wp_create_user( 'johndoe', 'passwordffgoeshere', '[email protected]' ); }
If you’re not familiar with hooks, we recommend you have a look at our Hooks Guide.
Create multiple users
Instead of repeating the above function several times, there’s a more efficient way to create multiple users at once.
The following function will create users from a given array, where names and passwords are stored.
add_action('admin_init','QuadLayers_create_users'); function QuadLayers_create_users(){ $users=array( array('johndoe','thepassword1'), array('tomdoe','thepassword2'), array('andrewdoe','thepassword3'), array('jeffdoe','thepassword4') ); foreach($users as $user){ wp_create_user( $user[0], $user[1] ); } }
To achieve this, we use a foreach()
loop to create the four users stored on the $users
array.
In this example, we’ve created four users but you can create as many as you want. Simply add them to the array and assign them a name and password.
Create a user and send an email
Now let’s see how you can create a user, generate a password and send it to a given email with a bit of code.
In the following script, we use two new functions:
- wp_generate_password() to generate a password
- wp_mail() to send the registration email to the recipient
add_action('admin_init','QuadLayers_create_user_send_mail'); function QuadLayers_create_user_send_mail(){ $password=wp_generate_password( 12, true ); wp_create_user('joedoe',$password); wp_mail( '[email protected]', 'Welcome!', 'Your password is: ' . $password ); }
This code will create a WordPress user programmatically, generate a password and send an email to [email protected] that says “Welcome! Your password is “. Make sure you customize the user, email, and message when you use it on your site.
Create user and assign account data
In this example, we are creating a user with attached information such as first name, last name, and role.
Note that we use a different function here. Instead of the wp_create_user()
function, we use wp_insert_user()
which is similar but more flexible and powerful.
add_action('admin_init','QuadLayers_create_user_meta'); function QuadLayers_create_user_meta(){ wp_insert_user( array( 'user_login' => 'janedoe', 'user_pass' => 'passwordhere', 'user_email' => '[email protected]', 'first_name' => 'Jane', 'last_name' => 'Doe', 'display_name' => 'Jane Doe', 'role' => 'editor' )); }
As you can see, in this example we’re creating a user and assigning a name (Jane), surname (Doe), email ([email protected]), role (editor), and so on.
Check if the user exists
This script is for validation and it’s useful in most cases, so we recommend you apply it to your code.
Here is how you can check if a user exists from a user name (janedoe in this example) using the username_exists()
function.
add_action('admin_init','QuadLayers_if_user_exists'); function QuadLayers_if_user_exists(){ if( null == username_exists( 'janedoe' ) ) { echo "not an Existing User";// Do something for unexistent user } else{ echo "Existing User"; // Do something if user name exists } }
This script marks users with two different texts depending on whether they exist or not:
- Users that don’t exist: “not an Existing User”
- Users that exist: “Existing User”
Conclusion
All in all, WordPress comes with features to create, manage, and delete different types of users. Even though this may be enough for most websites, if you want to have more options and be able to manage more permissions, it’s a good idea to create users using a bit of code.
In this guide, we’ve seen how to create a WordPress user programmatically and several examples to make the process more efficient. We’ve seen how to create multiple users at once, how to send them an email automatically, and assign data to them. Finally, we’ve added validation to check whether the user already exists or not. Now it’s time you customize these scripts and use them on your site!
Have you added users to your site using these scripts? How have you customized them? Let us know in the comments below!
For more options to improve your site with a bit of code, have a look at the following guides: