Changing the email WordPress sends administrators when passwords change

/ 15 July 2018

WordPress offers two little-known hooks for changing the email WordPress sends administrators and users when passwords are changed. These two filters (wp_password_change_notification and password_change_email) behave a little differently from one another. The biggest factor that caught me by surprise was the fact that wp_password_change_notification does not work at all when activated on a subsite of a multisite network (at least when that network is using subdirectories to distinguish sites).

Let’s define a small plugin called passworder to demonstrate how this filter can be used. Here is the passworder.php file that defines the plugin.

<?php
/*
 * Plugin Name: Passworder
 * Description: Modifies the email that goes out when passwords are changed.
 */

function password_change_email_admin( $email, $user, $blogname ) {
    $message = sprintf( __( 'Dear admin: password changed for %s: %s %s' ),
        $user->display_name,
        $user->ID,
        $user->user_login
 ) . "\r\n";
    error_log( $message );
    $email['message'] = $message;
    return $email;
}
add_filter( 'wp_password_change_notification_email', 'password_change_email_admin', 10, 3 );

Note that the wp_password_change_notification_email filter will not work at all unless it is network activated. This is because the whole password recovery process is executed at the network level and not within the bounds of a specific subsite. In this case, if the plugin were activated on a subsite, it would never be loaded and thus the filter would never be called.

This is not the case for the other filter, password_change_email, designed to send mail to the user when an admin changes their password using the profile. In this case the filter should be in a plugin activated on the subsite, not at the network level.