Log WordPress user login and logout activity with custom code

Log user logins and logouts without a plugin

For a small site with a few editors, a full activity log plugin is often more than needed, but knowing who logged in when, and from where, still helps: to spot a stolen account, to answer “who changed this”, or simply to see whether the editors use the site. Two hooks, wp_login and wp_logout, plus wp_login_failed for failed attempts, are enough for a basic log.

Problem: The administrator wants to see the last logins and logouts of each user, and failed login attempts, without installing an activity log plugin.

Solution: Append an entry to a user meta field on wp_login and wp_logout, keep only the last entries, store failed attempts separately, and show the log on the user’s profile screen.

Recording logins and logouts

<?php
function ha_add_login_log_entry( $user_id, $event ) {
    $log   = get_user_meta( $user_id, '_ha_login_log', true );
    $log   = is_array( $log ) ? $log : array();
    $log[] = array(
        'event' => $event,
        'time'  => time(),
        'ip'    => isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '',
    );

    update_user_meta( $user_id, '_ha_login_log', array_slice( $log, -30 ) ); // keep the last 30
}

add_action( 'wp_login', function ( $user_login, $user ) {
    ha_add_login_log_entry( $user->ID, 'login' );
}, 10, 2 );

add_action( 'wp_logout', function ( $user_id ) {
    ha_add_login_log_entry( $user_id, 'logout' );
} );

Since WordPress 5.5, wp_logout passes the user ID; on older versions use get_current_user_id() inside the callback, before the session is destroyed. The time is stored as a UTC timestamp and formatted on output.

Behind a proxy or a CDN, REMOTE_ADDR is the proxy’s address. The visitor’s IP is then in a header such as X-Forwarded-For, but trust it only when you know the proxy sets it, because any client can send that header.

Failed attempts

<?php
add_action( 'wp_login_failed', function ( $username ) {
    $failed   = get_option( 'ha_failed_logins', array() );
    $failed[] = array( 'user' => sanitize_user( $username ), 'time' => time() );
    update_option( 'ha_failed_logins', array_slice( $failed, -100 ), false ); // not autoloaded
} );

Many failed attempts for one username are a brute force attack; many different usernames from one IP are a bot scanning the site.

Showing the log on the profile

<?php
add_action( 'edit_user_profile', 'ha_show_login_log' );
add_action( 'show_user_profile', 'ha_show_login_log' );

function ha_show_login_log( $user ) {
    if ( ! current_user_can( 'list_users' ) ) {
        return;
    }

    $log = array_reverse( (array) get_user_meta( $user->ID, '_ha_login_log', true ) );

    echo '<h2>' . esc_html__( 'Recent logins', 'textdomain' ) . '</h2><table class="widefat striped"><tbody>';
    foreach ( array_filter( $log ) as $entry ) {
        printf(
            '<tr><td>%s</td><td>%s</td><td>%s</td></tr>',
            esc_html( $entry['event'] ),
            esc_html( wp_date( 'Y-m-d H:i', $entry['time'] ) ),
            esc_html( $entry['ip'] )
        );
    }
    echo '</tbody></table>';
}

NOTE: IP addresses are personal data under GDPR. Keep the log short (here 30 entries per user), mention it in the privacy policy, and include it in the personal data export and erasure. For sites with many users or strict audit requirements, a dedicated table or an activity log plugin is the better choice than user meta.