wp_new_user_notification

Email login credentials to a newly-registered user.

Description

wp_new_user_notification( (int) $user_id, (constant) $deprecated = null, (string) $notify = '' ); 

A new user registration notification is also sent to admin email.

Parameters (3)

0. $user_id (int)
The user id.
1. $deprecated — Optional. (constant) => null
Not used (argument deprecated).
2. $notify — Optional. (string) => ''
Type of notification that should happen. Accepts admin or an empty string (admin only), user,, or both (admin and user). Default empty.

Usage

  1. if ( !function_exists( 'wp_new_user_notification' ) ) { 
  2. require_once ABSPATH . WPINC . '/pluggable.php'; 
  3.  
  4. // The user id. 
  5. $user_id = -1; 
  6.  
  7. // Not used (argument deprecated). 
  8. $deprecated = null; 
  9.  
  10. // Optional. Type of notification that should happen. Accepts 'admin' or an empty 
  11. // string (admin only), 'user', or 'both' (admin and user). Default empty. 
  12. $notify = ''; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = wp_new_user_notification($user_id, $deprecated, $notify); 
  16.  

Defined (1)

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

/wp-includes/pluggable.php  
  1. function wp_new_user_notification( $user_id, $deprecated = null, $notify = '' ) { 
  2. if ( $deprecated !== null ) { 
  3. _deprecated_argument( __FUNCTION__, '4.3.1' ); 
  4.  
  5. global $wpdb, $wp_hasher; 
  6. $user = get_userdata( $user_id ); 
  7.  
  8. // The blogname option is escaped with esc_html on the way into the database in sanitize_option 
  9. // we want to reverse this for the plain text arena of emails. 
  10. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 
  11.  
  12. if ( 'user' !== $notify ) { 
  13. $switched_locale = switch_to_locale( get_locale() ); 
  14. $message = sprintf( __( 'New user registration on your site %s:' ), $blogname ) . "\r\n\r\n"; 
  15. $message .= sprintf( __( 'Username: %s' ), $user->user_login ) . "\r\n\r\n"; 
  16. $message .= sprintf( __( 'Email: %s' ), $user->user_email ) . "\r\n"; 
  17.  
  18. @wp_mail( get_option( 'admin_email' ), sprintf( __( '[%s] New User Registration' ), $blogname ), $message ); 
  19.  
  20. if ( $switched_locale ) { 
  21.  
  22. // `$deprecated was pre-4.3 `$plaintext_pass`. An empty `$plaintext_pass` didn't sent a user notification. 
  23. if ( 'admin' === $notify || ( empty( $deprecated ) && empty( $notify ) ) ) { 
  24. return; 
  25.  
  26. // Generate something random for a password reset key. 
  27. $key = wp_generate_password( 20, false ); 
  28.  
  29. /** This action is documented in wp-login.php */ 
  30. do_action( 'retrieve_password_key', $user->user_login, $key ); 
  31.  
  32. // Now insert the key, hashed, into the DB. 
  33. if ( empty( $wp_hasher ) ) { 
  34. $wp_hasher = new PasswordHash( 8, true ); 
  35. $hashed = time() . ':' . $wp_hasher->HashPassword( $key ); 
  36. $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) ); 
  37.  
  38. $switched_locale = switch_to_locale( get_user_locale( $user ) ); 
  39.  
  40. $message = sprintf(__('Username: %s'), $user->user_login) . "\r\n\r\n"; 
  41. $message .= __('To set your password, visit the following address:') . "\r\n\r\n"; 
  42. $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user->user_login), 'login') . ">\r\n\r\n"; 
  43.  
  44. $message .= wp_login_url() . "\r\n"; 
  45.  
  46. wp_mail($user->user_email, sprintf(__('[%s] Your username and password info'), $blogname), $message); 
  47.  
  48. if ( $switched_locale ) {