retrieve_password

Handles sending password retrieval email to user.

Description

(bool|WP_Error) retrieve_password(); 

Returns (bool|WP_Error)

True: when finish. WP_Error on error


Usage

  1. if ( !function_exists( 'retrieve_password' ) ) { 
  2. require_once '/wp-login.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = retrieve_password(); 
  7.  

Defined (1)

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

/wp-login.php  
  1. function retrieve_password() { 
  2. $errors = new WP_Error(); 
  3.  
  4. if ( empty( $_POST['user_login'] ) ) { 
  5. $errors->add('empty_username', __('<strong>ERROR</strong>: Enter a username or email address.')); 
  6. } elseif ( strpos( $_POST['user_login'], '@' ) ) { 
  7. $user_data = get_user_by( 'email', trim( wp_unslash( $_POST['user_login'] ) ) ); 
  8. if ( empty( $user_data ) ) 
  9. $errors->add('invalid_email', __('<strong>ERROR</strong>: There is no user registered with that email address.')); 
  10. } else { 
  11. $login = trim($_POST['user_login']); 
  12. $user_data = get_user_by('login', $login); 
  13.  
  14. /** 
  15. * Fires before errors are returned from a password reset request. 
  16. * @since 2.1.0 
  17. * @since 4.4.0 Added the `$errors` parameter. 
  18. * @param WP_Error $errors A WP_Error object containing any errors generated 
  19. * by using invalid credentials. 
  20. */ 
  21.  
  22. if ( $errors->get_error_code() ) 
  23. return $errors; 
  24.  
  25. if ( !$user_data ) { 
  26. $errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or email.')); 
  27. return $errors; 
  28.  
  29. // Redefining user_login ensures we return the right case in the email. 
  30. $user_login = $user_data->user_login; 
  31. $user_email = $user_data->user_email; 
  32. $key = get_password_reset_key( $user_data ); 
  33.  
  34. if ( is_wp_error( $key ) ) { 
  35. return $key; 
  36.  
  37. $message = __('Someone has requested a password reset for the following account:') . "\r\n\r\n"; 
  38. $message .= network_home_url( '/' ) . "\r\n\r\n"; 
  39. $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; 
  40. $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n"; 
  41. $message .= __('To reset 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_login), 'login') . ">\r\n"; 
  43.  
  44. if ( is_multisite() ) { 
  45. $blogname = get_network()->site_name; 
  46. } else { 
  47. /** 
  48. * The blogname option is escaped with esc_html on the way into the database 
  49. * in sanitize_option we want to reverse this for the plain text arena of emails. 
  50. */ 
  51. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 
  52.  
  53. /** translators: Password reset email subject. 1: Site name */ 
  54. $title = sprintf( __('[%s] Password Reset'), $blogname ); 
  55.  
  56. /** 
  57. * Filters the subject of the password reset email. 
  58. * @since 2.8.0 
  59. * @since 4.4.0 Added the `$user_login` and `$user_data` parameters. 
  60. * @param string $title Default email title. 
  61. * @param string $user_login The username for the user. 
  62. * @param WP_User $user_data WP_User object. 
  63. */ 
  64. $title = apply_filters( 'retrieve_password_title', $title, $user_login, $user_data ); 
  65.  
  66. /** 
  67. * Filters the message body of the password reset mail. 
  68. * @since 2.8.0 
  69. * @since 4.1.0 Added `$user_login` and `$user_data` parameters. 
  70. * @param string $message Default mail message. 
  71. * @param string $key The activation key. 
  72. * @param string $user_login The username for the user. 
  73. * @param WP_User $user_data WP_User object. 
  74. */ 
  75. $message = apply_filters( 'retrieve_password_message', $message, $key, $user_login, $user_data ); 
  76.  
  77. if ( $message && !wp_mail( $user_email, wp_specialchars_decode( $title ), $message ) ) 
  78. wp_die( __('The email could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function.') ); 
  79.  
  80. return true;