Two WordPress Plugins To Improve Your User Management Page

This is a two part post that will give you the code for plugins that will modify your WordPress user management page with some useful additional information

wp-users

This post will give you the code for the highlighted area on the left. This code adds two bits of information for every user, their last login date and time, and the last date and time they they were active. When you hover over the “Last login” and “Last active” text the tooltip shows you the exact date and time info.

<?php
/**
* Plugin Name: Last Login
* Description: This plugin stores the last login time for every user and displays it on the Users page.
* Version: 1.0.0
* Author: catchmyfame.com
*/

function catchmyfame_last_login( $user_login, $user ){
 update_user_meta( $user->ID, 'catchmyfame_last_login', current_time( 'mysql' ) );
}
function catchmyfame_last_active(){
 $current_user = wp_get_current_user();
 update_user_meta( $current_user->ID, 'catchmyfame_last_active', current_time( 'mysql' ) );
}
add_action( 'wp_login', 'catchmyfame_last_login', 10, 2);
add_action( 'admin_init', 'catchmyfame_last_active', 1);

add_filter( 'manage_users_columns', function( $columns ) {
 return array_slice( $columns, 0, 2, true ) + [ 'mycol' => __( 'Name' ) ] + array_slice( $columns, 3, null, true );
} );

add_filter( 'manage_users_custom_column', function( $output, $column_name, $user_id ) {
 if( 'mycol' === $column_name ) {
 $u = new WP_User( $user_id ); 
 if( $u instanceof \WP_User ) {
 // Default output
 $output .= "$u->first_name $u->last_name ($user_id)";

 // Additional output
 $last_login = get_user_meta( $user_id, 'catchmyfame_last_login', true );
 $last_active = get_user_meta( $user_id, 'catchmyfame_last_active', true );
 $login_time = 'Never logged in';
 if( !empty( $last_login ) ) {
 $login_time = human_time_diff( mysql2date( 'U', $last_login ), current_time('timestamp') ) . " ago";
 $active_time = human_time_diff( mysql2date( 'U', $last_active ), current_time('timestamp') ) . " ago";
 $output .= "<p style=\"font-size:11px; color:#999\">Last login: <span title=\"".mysql2date( 'l, F j, Y @ g:i a ', $last_login )."\">$login_time</span>\n";
 $output .= "- Last active: <span title=\"".mysql2date( 'l, F j, Y @ g:i a ', $last_active )."\">$active_time</span></p>\n";
 } else {
 $output .= "<p style=\"font-size:11px; color:#999\">Last login: <span title=\"".mysql2date( 'l, F j, Y @ g:i a ', $last_login )."\">$login_time</span>\n";
 }

 // Housekeeping
 unset( $u );
 }
 } 
 return $output;
}, 11, 3 ); 

add_filter( 'manage_users_sortable_columns', function( $columns ) {
 $columns['mycol'] = 'name';
 return $columns;
} );
?>

My next post will focus on the other enhancement to the user’s page, a new column on the right that lists all the pages each user has, broken down by types, with links to each.

3 thoughts on “Two WordPress Plugins To Improve Your User Management Page”

Leave a Reply

Your email address will not be published. Required fields are marked *