Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.