register_new_user

Handles registering a new user.

Description

(int|WP_Error) register_new_user( (string) $user_login, (string) $user_email ); 

Returns (int|WP_Error)

Either user's ID or error on failure.

Parameters (2)

0. $user_login (string)
User's username for logging in
1. $user_email (string)
User's email address to send password and add

Usage

  1. if ( !function_exists( 'register_new_user' ) ) { 
  2. require_once ABSPATH . WPINC . '/user.php'; 
  3.  
  4. // User's username for logging in 
  5. $user_login = ''; 
  6.  
  7. // User's email address to send password and add 
  8. $user_email = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = register_new_user($user_login, $user_email); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/user.php  
  1. function register_new_user( $user_login, $user_email ) { 
  2. $errors = new WP_Error(); 
  3.  
  4. $sanitized_user_login = sanitize_user( $user_login ); 
  5. /** 
  6. * Filters the email address of a user being registered. 
  7. * @since 2.1.0 
  8. * @param string $user_email The email address of the new user. 
  9. */ 
  10. $user_email = apply_filters( 'user_registration_email', $user_email ); 
  11.  
  12. // Check the username 
  13. if ( $sanitized_user_login == '' ) { 
  14. $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) ); 
  15. } elseif ( ! validate_username( $user_login ) ) { 
  16. $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) ); 
  17. $sanitized_user_login = ''; 
  18. } elseif ( username_exists( $sanitized_user_login ) ) { 
  19. $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) ); 
  20.  
  21. } else { 
  22. /** This filter is documented in wp-includes/user.php */ 
  23. $illegal_user_logins = array_map( 'strtolower', (array) apply_filters( 'illegal_user_logins', array() ) ); 
  24. if ( in_array( strtolower( $sanitized_user_login ), $illegal_user_logins ) ) { 
  25. $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: Sorry, that username is not allowed.' ) ); 
  26.  
  27. // Check the email address 
  28. if ( $user_email == '' ) { 
  29. $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your email address.' ) ); 
  30. } elseif ( ! is_email( $user_email ) ) { 
  31. $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn’t correct.' ) ); 
  32. $user_email = ''; 
  33. } elseif ( email_exists( $user_email ) ) { 
  34. $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) ); 
  35.  
  36. /** 
  37. * Fires when submitting registration form data, before the user is created. 
  38. * @since 2.1.0 
  39. * @param string $sanitized_user_login The submitted username after being sanitized. 
  40. * @param string $user_email The submitted email. 
  41. * @param WP_Error $errors Contains any errors with submitted username and email,  
  42. * e.g., an empty field, an invalid username or email,  
  43. * or an existing username or email. 
  44. */ 
  45. do_action( 'register_post', $sanitized_user_login, $user_email, $errors ); 
  46.  
  47. /** 
  48. * Filters the errors encountered when a new user is being registered. 
  49. * The filtered WP_Error object may, for example, contain errors for an invalid 
  50. * or existing username or email address. A WP_Error object should always returned,  
  51. * but may or may not contain errors. 
  52. * If any errors are present in $errors, this will abort the user's registration. 
  53. * @since 2.1.0 
  54. * @param WP_Error $errors A WP_Error object containing any errors encountered 
  55. * during registration. 
  56. * @param string $sanitized_user_login User's username after it has been sanitized. 
  57. * @param string $user_email User's email. 
  58. */ 
  59. $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email ); 
  60.  
  61. if ( $errors->get_error_code() ) 
  62. return $errors; 
  63.  
  64. $user_pass = wp_generate_password( 12, false ); 
  65. $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email ); 
  66. if ( ! $user_id || is_wp_error( $user_id ) ) { 
  67. $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn’t register you… please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) ); 
  68. return $errors; 
  69.  
  70. update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag. 
  71.  
  72. /** 
  73. * Fires after a new user registration has been recorded. 
  74. * @since 4.4.0 
  75. * @param int $user_id ID of the newly registered user. 
  76. */ 
  77.  
  78. return $user_id;