customization - Change Notice of Password change email subject?

admin2025-06-02  2

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!

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!

Share Improve this question asked Feb 27, 2019 at 14:28 joq3joq3 3813 silver badges21 bronze badges 1
  • 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 Commented Feb 27, 2019 at 15:00
Add a comment  | 

2 Answers 2

Reset to default 2

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.)

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 );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748858992a314312.html

最新回复(0)