Recording users last login

A useful little plugin to log the last login time of a user.

Grab the zip file or dump the code into a theme functions.php or your own plugin.  If you keep the metafield name ‘ausers_last_login’, the amr-users plugin will automatically create a nice name ‘Last Login’ for it.   You can then have the last login appear in your user reports.

Note:  some member, login and registration plugins affect the login action and prevent firing of wp-login action calls that have a lower / later priority. (eg: YM has a login action with priority ‘1’ that forces a redirect. To ensure that this wp_login action is run, the priority of the “add action” has been set to ‘0’.

Zip: auser-loginlogger.php

or code:

<?php    
/*
Plugin Name: amr-login-logger
Plugin URI: https://wpusersplugin.com/
Author URI: http://anmari.com
Description: if activated, will log the last login of each user.
Author: anmari
Version: 1.1
*/
	add_action('wp_login','ausers_loginlogger', 0, 2);

	function ausers_loginlogger($login, $user) {  

		$user = get_userdatabylogin($login);
		$now = date_create();
		update_usermeta( $user->ID, 'ausers_last_login', $now->format('Y-m-d h:i:s e l') );
	}
 ?>