Programmatically create a WordPress User

If you have access to the theme files and you wish to create a new WordPress user without having administrator access, then you can do so via functions.php.

Simply use this function:

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( 'contributor' );


} // end if

Source:https://tommcfarlin.com/create-a-user-in-wordpress/

Was this article helpful?

Related Articles