Changeset 3298796
- Timestamp:
- 05/22/2025 01:09:12 PM (10 months ago)
- Location:
- last-login-info/trunk
- Files:
-
- 2 edited
-
last-login-info.php (modified) (3 diffs)
-
readme.txt (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
last-login-info/trunk/last-login-info.php
r3279867 r3298796 1 1 <?php 2 2 3 /** 3 4 * Plugin Name: Last Login Info 4 5 * Description: Adds a column in the Users screen to show the last login date of each user. 5 * Version: 1. 0.06 * Version: 1.1.0 6 7 * Author: Hardik 7 8 * Author URI: https://profiles.wordpress.org/hardikhuptechdev/ … … 12 13 */ 13 14 14 if ( ! defined( 'ABSPATH' )) exit;15 if (! defined('ABSPATH')) exit; 15 16 16 // Hook into login action 17 add_action( 'wp_login', 'lastlogininfo_record_last_login', 10, 2 ); 18 function lastlogininfo_record_last_login( $user_login, $user ) { 19 update_user_meta( $user->ID, 'last_login', current_time( 'timestamp' ) ); 17 // Settings defaults 18 function lastlogininfo_get_settings() 19 { 20 return wp_parse_args( 21 get_option('lastlogininfo_settings', []), 22 [ 23 'date_format' => 'human', // or 'full' 24 'excluded_roles' => [] 25 ] 26 ); 20 27 } 21 28 22 // Add custom column to Users table 23 add_filter( 'manage_users_columns', 'lastlogininfo_add_last_login_column' ); 24 function lastlogininfo_add_last_login_column( $columns ) { 25 $columns['last_login'] = __( 'Last Login', 'last-login-info' ); 29 // Admin settings page 30 add_action('admin_menu', 'lastlogininfo_add_settings_page'); 31 function lastlogininfo_add_settings_page() 32 { 33 add_options_page( 34 'Last Login Info Settings', 35 'Last Login Info', 36 'manage_options', 37 'last-login-info', 38 'lastlogininfo_render_settings_page' 39 ); 40 } 41 42 function lastlogininfo_render_settings_page() 43 { 44 $roles = wp_roles()->roles; 45 $settings = lastlogininfo_get_settings(); 46 ?> 47 <div class="wrap"> 48 <h1><?php _e('Last Login Info Settings', 'last-login-info'); ?></h1> 49 <form method="post" action="options.php"> 50 <?php settings_fields('lastlogininfo_settings_group'); ?> 51 <?php do_settings_sections('last-login-info'); ?> 52 53 <table class="form-table" role="presentation"> 54 <tr> 55 <th scope="row"><?php _e('Date Display Format', 'last-login-info'); ?></th> 56 <td> 57 <select name="lastlogininfo_settings[date_format]"> 58 <option value="human" <?php selected($settings['date_format'], 'human'); ?>><?php _e('Human-readable (e.g., "2 days ago")', 'last-login-info'); ?></option> 59 <option value="full" <?php selected($settings['date_format'], 'full'); ?>><?php _e('Full Timestamp', 'last-login-info'); ?></option> 60 </select> 61 </td> 62 </tr> 63 <tr> 64 <th scope="row"><?php _e('Exclude Roles from Tracking', 'last-login-info'); ?></th> 65 <td> 66 <?php foreach ($roles as $key => $role) : ?> 67 <label> 68 <input type="checkbox" name="lastlogininfo_settings[excluded_roles][]" value="<?php echo esc_attr($key); ?>" <?php checked(in_array($key, $settings['excluded_roles'], true)); ?> /> 69 <?php echo esc_html($role['name']); ?> 70 </label><br> 71 <?php endforeach; ?> 72 </td> 73 </tr> 74 </table> 75 76 <?php submit_button(); ?> 77 </form> 78 </div> 79 <?php 80 } 81 82 add_action('admin_init', 'lastlogininfo_register_settings'); 83 function lastlogininfo_register_settings() 84 { 85 register_setting('lastlogininfo_settings_group', 'lastlogininfo_settings'); 86 } 87 88 // Record last login 89 add_action('wp_login', 'lastlogininfo_record_last_login', 10, 2); 90 function lastlogininfo_record_last_login($user_login, $user) 91 { 92 $settings = lastlogininfo_get_settings(); 93 $excluded_roles = $settings['excluded_roles']; 94 foreach ($user->roles as $role) { 95 if (in_array($role, $excluded_roles, true)) { 96 return; 97 } 98 } 99 100 update_user_meta($user->ID, 'last_login', current_time('timestamp')); 101 } 102 103 // Add column 104 add_filter('manage_users_columns', 'lastlogininfo_add_last_login_column'); 105 function lastlogininfo_add_last_login_column($columns) 106 { 107 $columns['last_login'] = __('Last Login', 'last-login-info'); 26 108 return $columns; 27 109 } 28 110 29 // Show the last login date in the column 30 add_filter( 'manage_users_custom_column', 'lastlogininfo_show_last_login', 10, 3 ); 31 function lastlogininfo_show_last_login( $output, $column_name, $user_id ) { 32 if ( 'last_login' === $column_name ) { 33 $timestamp = get_user_meta( $user_id, 'last_login', true ); 34 if ( $timestamp ) { 35 $time_diff = human_time_diff( $timestamp, current_time( 'timestamp' ) ); 36 // translators: %s is a human-readable time difference (e.g., "2 days") 37 return sprintf( __( '%s ago', 'last-login-info' ), $time_diff ); 111 // Display column 112 add_filter('manage_users_custom_column', 'lastlogininfo_show_last_login', 10, 3); 113 function lastlogininfo_show_last_login($output, $column_name, $user_id) 114 { 115 if ('last_login' === $column_name) { 116 $timestamp = get_user_meta($user_id, 'last_login', true); 117 if ($timestamp) { 118 $settings = lastlogininfo_get_settings(); 119 if ($settings['date_format'] === 'full') { 120 return date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $timestamp); 121 } else { 122 $time_diff = human_time_diff($timestamp, current_time('timestamp')); 123 // translators: %s is a human-readable time difference (e.g., "2 days") 124 return sprintf(__('%s ago', 'last-login-info'), $time_diff); 125 } 38 126 } else { 39 return __( 'Never', 'last-login-info');127 return __('Never', 'last-login-info'); 40 128 } 41 129 } … … 43 131 } 44 132 45 // Make the column sortable 46 add_filter( 'manage_users_sortable_columns', 'lastlogininfo_sortable_last_login_column' ); 47 function lastlogininfo_sortable_last_login_column( $columns ) { 133 // Sortable column 134 add_filter('manage_users_sortable_columns', 'lastlogininfo_sortable_last_login_column'); 135 function lastlogininfo_sortable_last_login_column($columns) 136 { 48 137 $columns['last_login'] = 'last_login'; 49 138 return $columns; 50 139 } 51 140 52 // Handle sorting logic 53 add_action( 'pre_get_users', 'lastlogininfo_sort_users_by_last_login' ); 54 function lastlogininfo_sort_users_by_last_login( $query ){55 if ( isset( $_REQUEST['orderby'] ) && $_REQUEST['orderby'] === 'last_login') {56 $query->set( 'meta_key', 'last_login');57 $query->set( 'orderby', 'meta_value_num');141 add_action('pre_get_users', 'lastlogininfo_sort_users_by_last_login'); 142 function lastlogininfo_sort_users_by_last_login($query) 143 { 144 if (isset($_REQUEST['orderby']) && $_REQUEST['orderby'] === 'last_login') { 145 $query->set('meta_key', 'last_login'); 146 $query->set('orderby', 'meta_value_num'); 58 147 } 59 148 } -
last-login-info/trunk/readme.txt
r3279867 r3298796 5 5 Tested up to: 6.8 6 6 Requires PHP: 7.2 7 Stable tag: 1. 0.07 Stable tag: 1.1.0 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html 10 10 11 Displays the last login timestamp of each user in the WordPress admin Users table .11 Displays the last login timestamp of each user in the WordPress admin Users table, with a settings page for customization. 12 12 13 13 == Description == … … 15 15 **Last Login Info** is a lightweight plugin that helps administrators see when users last logged into the site. 16 16 17 This plugin adds a "Last Login" column to the Users table in the WordPress admin. It tracks and displays each user's last login time, with human-readable formatting (e.g., "2 days ago"). The column is alsosortable, making it easier to find inactive users.17 This plugin adds a "Last Login" column to the Users table in the WordPress admin. It tracks and displays each user's last login time, with either human-readable formatting (e.g., "2 days ago") or full date/timestamp. The column is sortable, making it easier to find inactive users. 18 18 19 No settings required — just install, activate, and you're done! 19 A simple settings page is available under **Settings → Last Login Info**, where you can choose the date display format and exclude specific user roles from being tracked. 20 20 21 21 == Features == … … 23 23 * Adds a new "Last Login" column to the Users admin screen. 24 24 * Tracks login time on successful user login. 25 * Displays login timestamp in a human-readable format. 25 * Choose between human-readable or full timestamp display. 26 * Exclude specific roles from being tracked (e.g., Administrator). 26 27 * Sortable column by last login time. 27 28 * Lightweight, no bloat. … … 32 33 2. Activate the plugin through the 'Plugins' screen in WordPress. 33 34 3. Go to "Users" in the WordPress admin — you’ll see a new "Last Login" column. 35 4. Visit **Settings → Last Login Info** to configure display options. 34 36 35 37 == Frequently Asked Questions == … … 45 47 = Can I reset the login data? = 46 48 47 Currently, there is no settings page. You can remove the `last_login` user meta manually via custom code or a plugin like Advanced Custom Fields or WP-CLI.49 Not yet. This may be added in a future update. You can currently remove the `last_login` user meta manually via custom code or plugins like WP-CLI or Advanced Custom Fields. 48 50 49 51 == Screenshots == 50 52 51 53 1. Last Login column in the Users admin table. 54 2. Settings page with display and role options. 52 55 53 56 == Changelog == 57 58 = 1.1.0 = 59 * Added a settings page under "Settings → Last Login Info". 60 * Admins can now toggle between human-readable and full timestamp display. 61 * Option to exclude specific user roles (e.g., Administrator, Editor) from login tracking. 54 62 55 63 = 1.0.0 = … … 58 66 == Upgrade Notice == 59 67 68 = 1.1.0 = 69 Adds a settings page with date format selection and role exclusion features. 70 60 71 = 1.0.0 = 61 72 Initial release of Last Login Info.
Note: See TracChangeset
for help on using the changeset viewer.