How To Programmatically Create a WordPress User

How can I create a WordPress User programmatically from plugin? What functions should I use

To create a WordPress user programmatically,

1. Have to check if user exist or not by email
2. Generating the password for new user
3. Assigning the role to new user

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( 'author' );
}
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments