[Resolved] Best way to save user’s last login date?
This thread is resolved. Here is a description of the problem and solution.
Problem:
How to record the date a user last logged in?
Solution:
The WordPress hook wp_login is triggered whether a user logs in with the standard WordPress login form, or a Toolset custom login form, and can be used to trigger code to record the login time.
The client shows their code in this thread.
This support ticket is created 5 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
I just ran some quick tests to send a message to my debug.log whenever someone logs in, using the wp_login hook, and it worked fine, both when using the standard WP login form, and when using a Toolset custom login form (which I expected, as under the hood it uses the same standard WP functions).
The wp_login hook has the user_login and the user object available as arguments, so your code can be simplified.
The official hook documentation includes a link to a post about doing exactly what you want, the only thing you need to change is the post meta key:
Here is my final functions.php code in my child theme folder:
function store_member_last_login( $user_login, $user ) {
date_default_timezone_set('America/Los_Angeles'); // need to set timezone first
$todaysDateTime = date("M j, Y, g:i:s A"); // get the current time
// get the member-profile post_id corresponding to this user
$mbrProfileId = get_user_meta( $user->ID, 'wpcf-member-profile-id' , true );
// update the custom field "member-last-login" in CPT member-profile
update_post_meta($mbrProfileId, 'wpcf-member-last-login', $todaysDateTime);
}
add_action( 'wp_login', 'store_member_last_login', 10, 2 );