Using the wp_login hook, we could save an users login time. One usage of saving login time would be, to check who were recently logged-in to the site.
wp_login
action is called when a user successfully authenticate using his username/password. This action provides two information user_login (username) and userdata. And we will use this hook to save the time with the user id when user login.
add_action( 'wp_login', 'myplugin_wp_login', 10, 2); function myplugin_wp_login( $user_login, $user ) { update_user_meta( $user->ID, 'last_login', current_time('mysql') ); }
Here, we have used Three most important WordPress native function, add_action()
, update_user_meta()
& current_time()
.
- add_action() is used to bind an function on an pre specified event to do custom processing, It’s similar to JavaScript event binding.
- update_user_meta() is used to update WordPress User’s meta table information, with specific user id.
- current_time() is a similar function as like PHP’s time. difference is time() returns Unix localized timestamp, and
current_time()
return your WordPress Setup specific timestamp. WordPress site timezone can be changed from General Options page on WP Admin area.current_time()
accept one argument, and that can be used to return a formatted date rather than the timestamp, what we have actually used on our example here
You can also read about the usage of add_action
function here.
Next, we well be showing this piece of information on the users column (on wp admin). For that, We will add a new column. manage_users_columns
is the hook used to add addition table columns on Users table, and manage_users_custom_column
action is used to display additional column information.
add_filter( 'manage_users_columns', 'myplugin_manage_users_columns'); function myplugin_manage_users_columns( $columns ){ $columns['last_login'] = __('Last login', 'last_login'); return $columns; }
add_filter
is another most important function, it is used to sanitize, modify information using callback/hook. As like add_action, add_filter is also used on a pre specified events.
Now, to display the time information on the newly created column, use the following code
add_filter('manage_users_custom_column', 'myplugin_manage_users_custom_column', 10, 3); function myplugin_manage_users_custom_column( $value, $column_name, $user_id ){ $user = get_userdata( $user_id ); if ( 'last_login' == $column_name && $user->last_login) echo date("g:i a - d-M-y", strtotime( $user->last_login)); return $value; }
manage_users_custom_column
filter provides three information to its callback, ‘column_value’, ‘column_name’ & user_id.
Displaying Users login time – Complete Code
add_action( 'wp_login', 'myplugin_wp_login', 10, 2); function myplugin_wp_login( $user_login, $user ) { update_user_meta( $user->ID, 'last_login', current_time('mysql') ); } add_filter( 'manage_users_columns', 'myplugin_manage_users_columns'); function myplugin_manage_users_columns( $columns ) { $columns['last_login'] = __('Last login', 'last_login'); return $columns; } add_filter('manage_users_custom_column', 'myplugin_manage_users_custom_column', 10, 3); function myplugin_manage_users_custom_column( $value, $column_name, $user_id ) { $user = get_userdata( $user_id ); if ( 'last_login' == $column_name && $user->last_login) $value = date("g:i a - d-M-y", strtotime( $user->last_login)); return $value; }
After adding this on your plugin or theme’s functions.php file, you should see a new column on your WP Admin -> Users table.
Extending It More
Modifying the above code, you could add more columns and information. If you have any plugin/theme that is extending the default profile fields, and allowing user to add there social links (ex: facebook, twitter, google+ etc), you can create a column for that also. Quick reference –
add_filter( 'manage_users_columns', 'myplugin_manage_users_columns'); function myplugin_manage_users_columns( $columns ){ $columns['last_login'] = __('Last login', 'last_login'); $columns['social_links'] = __('Social Profile', 'last_login'); return $columns; } add_filter('manage_users_custom_column', 'myplugin_manage_users_custom_column', 10, 3); function myplugin_manage_users_custom_column( $value, $column_name, $user_id ) { $user = get_userdata( $user_id ); if ( 'last_login' == $column_name ){ $last_login = get_user_meta( $user_id, 'last_login', true ); return mysql2date( "g:i a - d-M-y", $last_login ); } if( 'social_links' == $column_name ){ // we assume this information is saved using facebook meta key $fb = get_user_meta( $user_id, 'facebook', true ); if( $fb ){ return '<a href="'. $fb .'">Facebook</a>'; } } return $value; }
So, finally users table should look like this –