# Email Alert When an Administrator Logs In

> Sends an email to the site admin address every time an administrator account logs in, with the time, IP address and browser.

- Type: PHP
- Runs: Run everywhere
- Category: [Security](https://wpide.com/library/category/security/)
- Tags: security, login, email, alerts
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/administrator-login-alert/

## Code

```php
add_action( 'wp_login', function ( $user_login, $user ) {
    if ( ! user_can( $user, 'manage_options' ) ) {
        return;
    }

    $ip      = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : 'unknown';
    $browser = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : 'unknown';
    $site    = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );

    $subject = sprintf( '[%s] Administrator login: %s', $site, $user->user_login );
    $message = sprintf(
        "The administrator account \"%s\" just logged in to %s.\n\nTime: %s\nIP address: %s\nBrowser: %s\n\nIf you do not recognize this login, change the password and review your users.",
        $user->user_login,
        home_url( '/' ),
        wp_date( 'Y-m-d H:i:s T' ),
        $ip,
        $browser
    );

    wp_mail( get_option( 'admin_email' ), $subject, $message );
}, 10, 2 );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Email Alert When an Administrator Logs In" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
