Skip Navigation

[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, 11 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by jeffS-2 5 years, 10 months ago.

Assisted by: Nigel.

Author
Posts
#1176402

Tell us what you are trying to do?
When a user logs in, I want to save the current date/time in a custom field in their CPT profile.

Is there any documentation that you are following?
Not really. I thought this would be simple using wp_login hook, but the hook never seems to fire.

I checked online for help but only found same problem I have with wp_login not firing.

My functions.php code:

function store_member_last_login() {
	if ( is_user_logged_in() ) {
		$current_user = wp_get_current_user();	
		//  Get the member profile ID from user_meta (stored in WP user profile custom field) 
		$mbrProfileId = get_user_meta( $current_user->ID, 'wpcf-member-profile-id' , true );
		date_default_timezone_set('America/Los_Angeles');
		$todaysDateTime = date("M j, Y, g:i:s A");
		// update profile with new login date/time
		update_post_meta($mbrProfileId, 'wpcf-member-last-login', $todaysDateTime); 
	} else {
		echo '<br/>No Login info found.<br/>';
		die;
	}
}
add_action( 'wp_login', 'store_member_last_login' );
#1176747

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Jeff

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:

hidden link

#1179381

My issue is resolved now. Thank you!

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 );