WordPress 後臺用戶列表顯示用戶登錄次數和時間

對於開放註冊的多用戶WordPress站點,我們需要瞭解用戶的一些訪問情況,比如統計 用戶註冊時間、最近一次登錄的時間 等,今天我們一起拓展一下,在 WordPress 後臺用戶列表 統計用戶的登錄次數、登錄總時間(分鐘)、平均每次登錄的時間,同時支援刪除和導出記錄。

log-user-stats-wpdaxue_com

如果你不喜歡折騰代碼,可以直接安裝 Log Users Stats 外掛,如果你不喜歡外掛,可以將下面的代碼(來自 Log Users Stats 外掛)添加到主題的 functions.php 即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * WordPress 後臺用戶列表顯示用戶登錄次數和時間
 * https://www.wpdaxue.com/log-user-stats.html
 */
add_action('wp_login','freeman_your_last_login_time');
function freeman_your_last_login_time($login) {
	global $user_ID;
	$user = get_user_by('login', $login);
	$time_start = time();
	update_user_meta($user->ID, 'start_time', $time_start);
}
 
add_action('wp_logout', 'freeman_get_time_on_logout');
function freeman_get_time_on_logout($user_id) {
	global $user_ID;
	$user = get_user_by('id', $user_ID);
	$time_end = time();
	$time_start = get_user_meta($user->ID, 'start_time', true);
	$total_time = (intval($time_end) - intval($time_start));
	$total_time = round($total_time/60);
	$total_all_time = get_user_meta($user->ID, 'total_time', true);
	$total_time = $total_all_time + $total_time;
	update_user_meta($user->ID, 'total_time', $total_time);
 
 
	$logged_in_amount = get_user_meta($user->ID, 'logged_in_amount', true);
	$logged_in_amount = $logged_in_amount + 1;
	update_user_meta($user->ID, 'logged_in_amount', $logged_in_amount);
 
	$average_time = ($total_time/$logged_in_amount);
	update_user_meta($user->ID, 'average_time', $average_time);
}
 
add_filter('manage_users_columns', 'freeman_add_user_minutes_column');
function freeman_add_user_minutes_column($columns) {
	$columns['total_time'] = 'Total Minutes';
	$columns['logged_in_amount'] = '# of Logins';
	$columns['average_time'] = 'Ave. Min./Login';
	return $columns;
}
 
add_action('manage_users_custom_column',  'freeman_show_user_minutes_column_content', 10, 3);
function freeman_show_user_minutes_column_content($value, $column_name, $user_id) {
	$output = " ";
	$user = get_userdata( $user_id );
	if ( 'total_time' == $column_name )
		$output .= ($user->total_time);
	if ( 'logged_in_amount' == $column_name )
		$output .= ($user->logged_in_amount);
	if ( 'average_time' == $column_name )
		$output .= ($user->average_time);
	return $output;
}
 
add_action('admin_footer', 'freeman_custom_user_buttons');
function freeman_custom_user_buttons() {
	$screen = get_current_screen();
	if ( $screen->id != "users" )   // Only add to users.php page
	return;
	echo "
	<script type=\"text/javascript\">
		jQuery(document).ready(function($) {
			$('<option>').val('del_user_meta').text('Delete User Logs').appendTo(\"select[name='action']\");
			$('<option>').val('export_user_meta').text('Export User Logs').appendTo(\"select[name='action']\");
		});
	</script>
	";
}
 
add_action('load-users.php', 'freeman_delete_users_info');
function freeman_delete_users_info() {
	if(isset($_GET['action']) && $_GET['action'] === 'del_user_meta') {  // Check if our custom action was selected
		$del_users = $_GET['users'];  // Get array of user id's which were selected for meta deletion
		if ($del_users) {  // If any users were selected
			foreach ($del_users as $del_user) {
				delete_user_meta($del_user, 'logged_in_amount');
				delete_user_meta($del_user, 'total_time');
				delete_user_meta($del_user, 'average_time');
			}
		}
	}
}
 
add_action('load-users.php', 'freeman_export_users_info');
function freeman_export_users_info() {
	if(isset($_GET['action']) && $_GET['action'] === 'export_user_meta') {
		$del_users = $_GET['users'];
		if ($del_users) {
			$fp = fopen('file.csv', 'w');
			$User_Name_Row = array("USERNAME", "Total Minutes", "# of Logins", "Ave. Min./Login");
			fputcsv($fp, $User_Name_Row);
			foreach ($del_users as $del_user) {
				$user_info = get_userdata($del_user);
				$user_name = ($user_info->user_login);
				$logged_in_amount = get_user_meta($del_user, 'logged_in_amount', true);
				$total_time = get_user_meta($del_user, 'total_time', true);
				$average_time = get_user_meta($del_user, 'average_time', true);
 
				$list = array (
					array ($user_name, $total_time, $logged_in_amount, $average_time)
					);
 
				foreach ($list as $fields) {
					fputcsv($fp, $fields);
				}
			}
		}
 
		fclose($fp);
 
		$file="file.csv"; //file location
		header('Content-Type: application/octet-stream');
		header('Content-Disposition: attachment; filename="'.basename($file).'"');
		header('Content-Length: ' . filesize($file));
		readfile($file);
	}
}

/**
* WordPress 後臺用戶列表顯示用戶登錄次數和時間
* https://www.wpdaxue.com/log-user-stats.html
*/
add_action(‘wp_login’,’freeman_your_last_login_time’);
function freeman_your_last_login_time($login) {
global $user_ID;
$user = get_user_by(‘login’, $login);
$time_start = time();
update_user_meta($user->ID, ‘start_time’, $time_start);
} add_action(‘wp_logout’, ‘freeman_get_time_on_logout’);
function freeman_get_time_on_logout($user_id) {
global $user_ID;
$user = get_user_by(‘id’, $user_ID);
$time_end = time();
$time_start = get_user_meta($user->ID, ‘start_time’, true);
$total_time = (intval($time_end) – intval($time_start));
$total_time = round($total_time/60);
$total_all_time = get_user_meta($user->ID, ‘total_time’, true);
$total_time = $total_all_time + $total_time;
update_user_meta($user->ID, ‘total_time’, $total_time); $logged_in_amount = get_user_meta($user->ID, ‘logged_in_amount’, true);
$logged_in_amount = $logged_in_amount + 1;
update_user_meta($user->ID, ‘logged_in_amount’, $logged_in_amount); $average_time = ($total_time/$logged_in_amount);
update_user_meta($user->ID, ‘average_time’, $average_time);
} add_filter(‘manage_users_columns’, ‘freeman_add_user_minutes_column’);
function freeman_add_user_minutes_column($columns) {
$columns[‘total_time’] = ‘Total Minutes’;
$columns[‘logged_in_amount’] = ‘# of Logins’;
$columns[‘average_time’] = ‘Ave. Min./Login’;
return $columns;
} add_action(‘manage_users_custom_column’, ‘freeman_show_user_minutes_column_content’, 10, 3);
function freeman_show_user_minutes_column_content($value, $column_name, $user_id) {
$output = " ";
$user = get_userdata( $user_id );
if ( ‘total_time’ == $column_name )
$output .= ($user->total_time);
if ( ‘logged_in_amount’ == $column_name )
$output .= ($user->logged_in_amount);
if ( ‘average_time’ == $column_name )
$output .= ($user->average_time);
return $output;
} add_action(‘admin_footer’, ‘freeman_custom_user_buttons’);
function freeman_custom_user_buttons() {
$screen = get_current_screen();
if ( $screen->id != "users" ) // Only add to users.php page
return;
echo "
<script type=\"text/javascript\">
jQuery(document).ready(function($) {
$(‘<option>’).val(‘del_user_meta’).text(‘Delete User Logs’).appendTo(\"select[name=’action’]\");
$(‘<option>’).val(‘export_user_meta’).text(‘Export User Logs’).appendTo(\"select[name=’action’]\");
});
</script>
";
} add_action(‘load-users.php’, ‘freeman_delete_users_info’);
function freeman_delete_users_info() {
if(isset($_GET[‘action’]) && $_GET[‘action’] === ‘del_user_meta’) { // Check if our custom action was selected
$del_users = $_GET[‘users’]; // Get array of user id’s which were selected for meta deletion
if ($del_users) { // If any users were selected
foreach ($del_users as $del_user) {
delete_user_meta($del_user, ‘logged_in_amount’);
delete_user_meta($del_user, ‘total_time’);
delete_user_meta($del_user, ‘average_time’);
}
}
}
} add_action(‘load-users.php’, ‘freeman_export_users_info’);
function freeman_export_users_info() {
if(isset($_GET[‘action’]) && $_GET[‘action’] === ‘export_user_meta’) {
$del_users = $_GET[‘users’];
if ($del_users) {
$fp = fopen(‘file.csv’, ‘w’);
$User_Name_Row = array("USERNAME", "Total Minutes", "# of Logins", "Ave. Min./Login");
fputcsv($fp, $User_Name_Row);
foreach ($del_users as $del_user) {
$user_info = get_userdata($del_user);
$user_name = ($user_info->user_login);
$logged_in_amount = get_user_meta($del_user, ‘logged_in_amount’, true);
$total_time = get_user_meta($del_user, ‘total_time’, true);
$average_time = get_user_meta($del_user, ‘average_time’, true); $list = array (
array ($user_name, $total_time, $logged_in_amount, $average_time)
); foreach ($list as $fields) {
fputcsv($fp, $fields);
}
}
} fclose($fp); $file="file.csv"; //file location
header(‘Content-Type: application/octet-stream’);
header(‘Content-Disposition: attachment; filename="’.basename($file).’"’);
header(‘Content-Length: ‘ . filesize($file));
readfile($file);
}
}

測試發現:登錄次數是安裝本文外掛或添加代碼後開始統計,登錄時間莫名其妙是一個很大的數,也不知是從哪裡的,暫時不深究瞭。

發佈留言