Last active
December 30, 2024 16:57
-
-
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();
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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