Create new user in WordPress with custom coding?

In this article, we will learn how to create new user in WordPress using custom coding. As a WordPress Developer, it’s a quite common task to add users using code at backend rather than registering it through the frontend.

Here I am sharing a handy code snippet which you can use to add user through coding easily in WordPress using email address only.

Code to create new user

// Firsty we check if email_address exists or not
 if(!username_exists( $email_address ) ) {

  // Generate the password for user
  $password = wp_generate_password( 14, false );

// Using wp_create_user to add new user using email and password
  $user_id = wp_create_user( $email_address, $password, $email_address );

  // Set the nickname for user to display when user is logged in. If you have name available you can set it as nickname
  wp_update_user(
    array(
      'ID'          =>    $user_id,
      'nickname'    =>    $email_address
    )
  );

  // Set the role for user
  $user = new WP_User( $user_id );

//Here we define role of User
  $user->set_role( 'subscriber' );

  // Email the user his new password
  wp_mail( $email_address, 'Welcome to Techyadda', 'Your Password to login on websie is : ' . $password );

}

WordPress Functions used for creating User

username_exists() : It checks whether username or email exists or not before we create new user

wp_generate_password() : It helps us to generate random password using wp_rand()

wp_create_user() : It helps us to generate new user using 3 parameters – username, password and email.

wp_update_user() : It helps us to update existing user and we use it to update nickname of user.

wp_update_user() : It helps us to update existing user and we use it to update nickname of user.

Where to use this Code

We can use above code in various ways but here I am explaining via simple example. Suppose we have custom post type students and we need user to be added automatically when we add new students post type. Here in example I am using ACF to enter email while adding students post type.

Create new user in Wordpress with custom coding? 1

Now we want to add user when we save this student post type. For this we will use WordPress action ‘save_post’ which runs when post is saved.

//Add User when new student created
    add_action('save_post','save_post_callback');
function save_post_callback($post_id){
    global $post; 
//Here we check post type students and if it not students we return true and exit function
    if ($post->post_type != 'students'){
        return;
    }
    $email_address=get_field('email',$post_id);
    if( null == username_exists( $email_address ) ) {

  // Generate the password and create the user
  $password = wp_generate_password( 12, false );
  $user_id = wp_create_user( $email_address, $password, $email_address );

  // Set the nickname
  wp_update_user(
    array(
      'ID'          =>    $user_id,
      'nickname'    =>    $email_address
    )
  );

  // Set the role
  $user = new WP_User( $user_id );
  $user->set_role( 'student' );

  // Email the user
  wp_mail( $email_address, 'Welcome!', 'Your Password: ' . $password );

} // end if

I hope this helps you understand how to use this code now. If you have any doubts or need some customizations please comment and I will help you surely.

Anmol Mathneja

I am Web Developer who loves sharing his work experiences with people. I provide services like Website Development, Website Designing and Digital Marketing. My areas of expertise include Core PHP, Codeigniter, Laravel, Wordpress, jQuery, Node JS, HTML, CSS.

Leave a Reply