How to create WordPress user programmatically

How to create WordPress user programmatically
55 Views

Are you seeking for alternatives to using the admin dashboard to create users? If so, this is the post for you. In this post, we’ll teach you how to create a WordPress user programmatically, as well as provide sample scripts for your site.

Most websites nowadays allow visitors to create an account and become registered members in order to receive perks such as special offers and discounts.

When a new user registers, the site collects profile information and assigns a role to that person. These roles enable you to grant various rights to each user, such as access to restricted material, presenting a distinct navigation menu for each role, having access to certain offers, and so on.

WordPress allows you to handle many sorts of users and includes comprehensive user management capabilities. You may add, update, and remove users and grant different rights to each one using the various roles that are accessible by default. This is sufficient for basic websites, but if you want a more comprehensive solution to manage various sorts of permissions, you may need to create WordPress users programmatically.

Let’s have a look at how you can accomplish this.

Code Examples for Creating WordPress Users

In this part, we’ll teach you how to utilise scripts to create users on your site programmatically. The scripts are included, as well as an explanation of what each portion of the code does.

Make a single user.
Creating a WordPress user programmatically is straightforward and takes only one line of code. The only needed values are name and password, which may be specified using the wp_create_user() method as follows:

<?php
wp_create_user( 'xyz', 'passwordgoeshere', 'xyz@example.com' );
?>

To create a user named xyz, paste this line of code into your child theme’s functions.php file.

On top of that, there are a few additional things we can do to improve the above code. For example, you should utilise a hook to prevent the user from being created while they are travelling around backend sites or reloading the screen. The revised code will appear as follows:

<?php
add_action( 'admin_init', 'cxc_create_custom_user_call_back' );
function cxc_create_custom_user_call_back(){
	wp_create_user( 'xyz', 'passwordgoeshere', 'xyz@example.com' );
}
?>

How to Set Up Multiple Users

Instead of repeatedly performing the above procedure, there is a more efficient approach to create several users at once.

The following code will generate users from a provided array of names and passwords.

<?php
add_action('admin_init','cxc_create_multiple_user_call_back');
function cxc_create_multiple_user_call_back(){

	$users_arr = array(
		array( 'abc','abcword1' ),
		array( 'cxc','cxcword2' ),
		array( 'gujjuplanet','gujjuplanetword3' ),
		array( 'xyz','xyzword4' ),
	);

	if( $users_arr ) {
		foreach( $users_arr as $user ){     
			wp_create_user( $user[0], $user[1] );
		}    
	}
}
?>

We’ve created four users in this example, but you may add as many as you wish. Simply add them to the array and name and password them.

How to Set Up a User and Send Email

Let’s look at how you can use code to create a user, generate a password, and send it to a specified email address.

We utilise two new methods in the following script:

  • wp_generate_password() to generate a password
  • wp_mail() to send the registration email to the recipient

This code will programmatically create a WordPress account, establish a password, and send an email to xyz@mail.com with the subject “Welcome! ” is your password. When using it on your site, make sure to customise the user, email, and message.

How to Create a User and Assign Account Information

In this example, we’re establishing a user with attributes like first name, last name, and role.

It’s worth noting that we’re using a separate function here. We use wp_insert_user() instead of wp_create_user(), which is comparable but more versatile and powerful.

<?php
add_action( 'admin_init', 'cxc_create_user_meta_data_call_back' );
function cxc_create_user_meta_data_call_back(){   
	wp_insert_user( array(
		'user_login' 	=> 'xyz',
		'user_pass' 	=> 'passwordhere',
		'user_email' 	=> 'xyz@example.com',
		'first_name' 	=> 'Xyz',
		'last_name' 	=> 'Abc',
		'display_name' 	=> 'Xyz Abc',
		'role' 			=> 'customer'
	));
}
?>

As you can see, we’re establishing a user and giving it a name (Xyz), surname (Abc), email (xyz@example.com), role (customer), and so on.

Check to see if the user exists.

This script is for validation and is beneficial in the majority of circumstances, thus we recommend that you use it in your code.

The username_exists() method may be used to determine whether a user exists based on a user name (xyz in this example).

<?php
add_action( 'admin_init', 'cxc_if_user_exists_call_back' );
function cxc_if_user_exists_call_back(){
	if( username_exists( 'xyz' ) == '' ) {
		// Do something for unexistent user
		echo "not an Existing User";
	} else {
		// Do something if user name exists
		echo "Existing User"; 
	}
}
?>

This script assigns two distinct messages to users based on whether they exist or not:

  • Non-existent users: “not an Existing User”
  • Existing users: “Existing User”

Was this article helpful?
YesNo

Leave a comment

Your email address will not be published.