Alert When an Administrator Password Reset Is Requested
Emails the site admin whenever someone requests a password reset for an administrator account, an early warning sign of an attempt to take over the site.
Opens WPIDE on
Code
add_action( 'retrieve_password', function ( $user_login ) {
$user = get_user_by( 'login', $user_login );
if ( ! $user || ! user_can( $user, 'manage_options' ) || get_transient( 'wpide_reset_alert_' . $user->ID ) ) {
return;
}
set_transient( 'wpide_reset_alert_' . $user->ID, 1, HOUR_IN_SECONDS );
$ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : 'unknown';
wp_mail(
get_option( 'admin_email' ),
sprintf( '[%s] Password reset requested for administrator %s', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $user->user_login ),
sprintf( "A password reset was requested for the administrator account \"%s\" on %s from IP %s.\n\nIf this was not expected, make sure the account's email inbox is secure; the password has not been changed.", $user->user_login, wp_date( 'Y-m-d H:i' ), $ip )
);
} );
Notes
Sends at most one alert per account per hour; the requester's IP address is included.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.