WordPress 后台用户列表显示用户登录次数和时间

乐观主义者从每一个灾难中看到机遇,而悲观主义都从每一个机遇中看到灾难。无论有多困难,都坚强地抬头挺胸,人生是一场醒悟,不要昨日,不要明天,只要今日。

对于开放注册的多用户WordPress站点,我们需要了解用户的一些访问情况,比如统计 用户注册时间、最近一次登录的时间 等,今天我们一起拓展一下,在 WordPress 后台用户列表 统计用户的登录次数、登录总时间(分钟)、平均每次登录的时间,同时支持删除和导出记录。

如果你不喜欢折腾代码,可以直接安装 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); } }

测试发现:登录次数是安装本文插件或添加代码后开始统计,登录时间莫名其妙是一个很大的数,也不知是从哪里的,暂时不深究了。

以上就是WordPress 后台用户列表显示用户登录次数和时间。善欲人知不是真善,恶恐人知便是大恶。更多关于WordPress 后台用户列表显示用户登录次数和时间请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
WordPress 强制用户登录以后才能阅读全文

WordPress 禁止某些用户登录

WordPress 限制用户登录尝试次数 Limit Login Attempts

WordPress 用户登录失败(账号或密码错误)重定向到自定义页面

WordPress 侧边栏 Ajax 无刷新用户登录/注册/修改密码