0

I'm having issues to change the subject of the email sent out when password is changed. I have managed to change the message by doing this:

// Change Email to HTML function
function set_email_html_content_type() {
    return 'text/html';
}

// Replace the default password change email
add_filter('password_change_email', 'change_password_mail_message', 10, 3);
function change_password_mail_message( $change_mail, $user, $userdata ) {
    // Call Change Email to HTML function
    add_filter( 'wp_mail_content_type', 'set_email_html_content_type' );
    $message = "<p>Test HTML email</p>";

    $change_mail[ 'message' ] = $message;
        return $change_mail;

    // Remove filter HTML content type
    remove_filter( 'wp_mail_content_type', 'set_email_html_content_type' );
}

But how do I change the subject which is default as [Sitename] Notice of Password Change

Thank you!

  • You don't need to make use of wp_mail_content_type filter as all you want to do and more can be achieved modifying the var $change_mail. Just check my answer. – filipecsweb Feb 27 at 15:00
1

The $change_mail variable that you're filtering has a subject value that you can modify the same way you modified the message:

// Replace the default password change email
add_filter('password_change_email', 'change_password_mail_message', 10, 3);
function change_password_mail_message( $change_mail, $user, $userdata ) {
    // Call Change Email to HTML function
    add_filter( 'wp_mail_content_type', 'set_email_html_content_type' );
    $message = "<p>Test HTML email</p>";

    $change_mail[ 'message' ] = $message;
    $change_mail[ 'subject' ] = 'My new email subject';

    return $change_mail;
}

(I removed your call to remove_filter() because it wouldn't do anything. Nothing after return will run.)

  • Oh it was that simple haha! I struggled with this, and tried everything except the easiest solution! Thank you for explaining it and the return too! – joq3 Feb 27 at 15:06
0

Try this:

/**
 * Hooked into `password_change_email` filter hook.
 *
 * @param array $pass_change_email {
 *                                 Used to build wp_mail().
 *
 * @type string $to                The intended recipients. Add emails in a comma separated string.
 * @type string $subject           The subject of the email.
 * @type string $message           The content of the email.
 *                The following strings have a special meaning and will get replaced dynamically:
 *                - ###USERNAME###    The current user's username.
 *                - ###ADMIN_EMAIL### The admin email in case this was unexpected.
 *                - ###EMAIL###       The user's email address.
 *                - ###SITENAME###    The name of the site.
 *                - ###SITEURL###     The URL to the site.
 * @type string $headers           Headers. Add headers in a newline (\r\n) separated string.
 *        }
 *
 * @param array $user              The original user array.
 * @param array $userdata          The updated user array.
 *
 * @return  array   $pass_change_email
 */
function change_password_mail_message( $pass_change_email, $user, $userdata ) {
    $subject   = "Your new subject";
    $message   = "<p>Test HTML email</p>";
    $headers[] = 'Content-Type: text/html';

    $pass_change_email['subject'] = $subject;
    $pass_change_email['message'] = $message;
    $pass_change_email['headers'] = $headers;

    return $pass_change_email;
}

/**
 * Filters the contents of the email sent when the user's password is changed.
 *
 * @see change_password_mail_message()
 */
add_filter( 'password_change_email', 'change_password_mail_message', 10, 3 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.