'Custom WordPress Reset Password E-Mail from Template

I use this function to customize the standard WordPress notification for each user registration.

function custom_wp_new_user_notification_email( $wp_new_user_notification_email, $user, $blogname ) {
      global $wpcargo;
        $user_url = stripslashes( $user->user_url );
        $user_login = stripslashes( $user->user_login );
        $user_email = stripslashes( $user->user_email );
        $user_firstname = stripslashes( $user->user_firstname );
        $user_last_name = stripslashes( $user->user_last_name );
        $user_pass = stripslashes( $user->user_pass );
        $message = file_get_contents('../email/mail-template.php');
        $wp_new_user_notification_email['subject'] = sprintf( '[%s] Welcome.', $blogname );
        $wp_new_user_notification_email['headers'] = array('Content-Type: text/html; charset=UTF-8');
        $wp_new_user_notification_email['message'] = $message; 
        return $wp_new_user_notification_email;
    }
    add_filter( 'wp_new_user_notification_email', 'custom_wp_new_user_notification_email', 10, 3 );

I have to use the same logic to customize the password reset email as well.

I currently use the following code:

function replace_retrieve_password_message( $message, $key, $user_login, $user_data ) {
  // Create new message
  $msg = __( 'Hello!' ) . "\r\n\r\n";
  $msg .= __( 'xxxxxx.' ) . "\r\n\r\n";
  $msg .= __( "xxxxxx." ) . "\r\n\r\n";
  $msg .= __( 'xxxxxx:' ) . "\r\n\r\n";
  $msg .= site_url( "wp-login.php?action=rp&key=$key&login=" . rawurlencode( $user_login ), 'login' ) . "\r\n\r\n";
  $msg .= __( 'Thanks!' ) . "\r\n";
  return $msg;
}
add_filter( 'retrieve_password_message', 'replace_retrieve_password_message', 10, 4 );

How can I change it to be similar to the first but using a different template?


I changed the function like this. Now send me the template correctly and generate the reset string for me. However, the user's Name does not appear and the subject of the email is the default one.

function replace_retrieve_password_message( $message, $key, $user_login, $user_data ) {
  // Create new message
  ob_start();
  include( locate_template( '/email/pwlost-email-template.php'));
  $msg = ob_get_clean();
  return $msg;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source