Wordpress主题技巧

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

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);
}
}

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

Wordpress主题技巧

更多有关于 Wordpress主题技巧

域名解析教程

Metaslider幻灯片使用详解!

塔克网整站模板 安装使用指南

WordPress如何备份网站数据库和文件?WordPress整站搬家操作流程。

WordPress网站安全设置教程

国外英文广告联盟网站模板,WordPress主题整站演示地址列表

WordPress主题开发:开启文章缩略图功能

WordPress 自定义头像插件:WP User Avatar

WordPress 后台管理界面UI自定义插件:WP Admin UI Customize

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2016 Tadke.com. Powered by Wordpress.