Skip to content

Instantly share code, notes, and snippets.

@dknauss
Last active December 30, 2024 16:57
Show Gist options
  • Select an option

  • Save dknauss/59ca633c5dc4f12e23d04faa2c3b9c4a to your computer and use it in GitHub Desktop.

Select an option

Save dknauss/59ca633c5dc4f12e23d04faa2c3b9c4a to your computer and use it in GitHub Desktop.
Log time() WordPress user session started by wp_login(); and ended with wp_logout();
// Log session login time when wp_login() is fired.
function user_session_start( $user_login, $user ) {
update_user_meta( $user->ID, 'last_login', time() );
return $user_login;
}
add_action( 'wp_login', 'user_session_start', 10, 2 );
// Log session logout time when wp_logout() is fired.
function user_session_logout($user_id){
update_user_meta( $user_id, 'last_logout', time() );
}
add_action( 'wp_logout', 'user_session_logout', 10, 1 );
// Note: WordPress core does not have a way to log every possible type of session termination.
// The user_session_logout hook will not let us log sessions ended by an auth cookie timeout or
// other failed session checks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment