性价比最高的香港ASP、PHP主机提供商
做网站,就上凡科建站

WordPress 统计每天登录的用户数量

Loading
0
阿里云代金券1000元礼包免费抢

让 WordPress 统计每天登录的用户数量,可以让你对每天的用户活跃程度有一个基本的了解。使用  User Login Stat 插件就可以做到这一点。它会在后台 > 设置 > User Login Stat  显示每天登录的用户数量。

user-login-stat-wpdaxue_com

在后台插件安装界面搜索 User Login Stat 即可在线安装,或者下载 User Login Stat

倡萌认为将该统计数据放在WP仪表盘显示会好很多,会代码的朋友也可以参考下 WordPress 仪表盘小工具接口(Dashboard Widgets API)

以下是 User Login Stat 插件的全部代码:

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/*
  Plugin Name: User Login Stats
  Plugin URI: http://tareq.weDevs.com/
  Description: Displays and monitors the user login statistics
  Author: Tareq Hasan
  Author URI: http://tareq.weDevs.com/
  Donate URI: http://tareq.weDevs.com/
  Version: 0.1
 */
 
class User_Login_Stats{
 
    private $table;
 
    function __construct() {
        global $wpdb;
 
        $this->table = $wpdb->prefix . "user_stats";
 
        register_activation_hook( __FILE__, array( &$this, 'install' ) );
        add_action( 'wp_login', array( &$this, 'login_update' ) );
        add_action( 'wp_head', array( &$this, 'check_user' ) );
        add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
    }
 
    /**
     * Create the table installing the plugin
     * 
     * @global type $wpdb 
     */
    function install() {
        global $wpdb;
 
        $sql = "CREATE TABLE IF NOT EXISTS `{$this->table}` (
             `id` int(11) NOT NULL AUTO_INCREMENT,
             `date` date NOT NULL,
             `count` int(11) NOT NULL,
             KEY `id` (`id`)
            ) ENGINE=MyISAM";
 
        $wpdb->query( $sql );
    }
 
    /**
     * Run the function when the user logs in
     * 
     * If the users last login date is not today, update the visit count
     * and update the last login
     * 
     * @param type $login 
     */
    function login_update( $login ) {
        $user = get_user_by( 'login', $login );
 
        $last_login = ( isset( $user->last_login ) ) ? strtotime( $user->last_login ) : 0;
        $last_login_date = date( 'Y-m-d', $last_login );
 
        //if the user already loggedin today, skip him
        if( $last_login_date != date( 'Y-m-d', time() ) ) {
            $this->update( $user->ID );
        }
    }
 
    /**
     * Runs everytime to check if the user's last login time is more than 24 hours
     * 
     * This function runs on `wp_head` hook and works for only loggedin users
     */
    function check_user() {
        $current_user = wp_get_current_user();
 
        if( $current_user->ID ) {
            $last_login = ( isset( $current_user->last_login ) ) ? strtotime( $current_user->last_login ) : 0;
 
            $duration = 24 * 60 * 60; //24hours
            if( (time() - $last_login ) > $duration ) {
                $this->update( $current_user->ID );
            }
        }
    }
 
    /**
     * Update the database and user last login
     * 
     * If a row is already in the table, increase the count. Otherwise create 
     * a new row and store 1 as the value
     * 
     * @global type $wpdb
     * @param type $user_id 
     */
    function update( $user_id ) {
        global $wpdb;
 
        //if any rows found, increase the count, else insert new row
        $today = date( 'Y-m-d', time() );
        $row = $wpdb->get_row( "SELECT `count` FROM {$this->table} WHERE `date`='$today'" );
        if( $row ) {
            $wpdb->query( "UPDATE {$this->table} SET `count`=`count`+1 WHERE `date`='$today'" );
        } else {
            $wpdb->insert( $this->table, array(
                'date' => $today,
                'count' => 1
            ) );
        }
 
        //update user last login
        update_user_meta( $user_id, 'last_login', gmdate( 'Y-m-d H:i:s' ) );
    }
 
    /**
     * Adds the admin panel menu to the settings main menu
     */
    function admin_menu() {
        add_submenu_page( 'options-general.php', 'User Login Stats', 'User Login Stats', 'administrator', 'user_stats', array( $this, 'admin_page' ) );
    }
 
    /**
     * Displays the statistics in the admin area
     * 
     * @global type $wpdb
     * @global type $userdata 
     */
    function admin_page() {
        global $wpdb, $userdata;
 
        $pagenum = ( isset( $_GET['pagenum'] ) ) ? absint( $_GET['pagenum'] ) : 1;
        $limit = 30;
        $offset = ( $pagenum - 1 ) * $limit;
 
        $sql = "SELECT * FROM {$this->table} ORDER BY `date` DESC LIMIT $offset, $limit";
        $table = $wpdb->get_results( $sql );
 
        $total_users = count_users();
 
        //{$this->table}
        $week = $wpdb->get_var( "SELECT sum( `count` ) FROM `{$this->table}` WHERE `date` >= ( DATE_SUB( CURRENT_DATE, INTERVAL 7 DAY ) )" );
        $month = $wpdb->get_var();
        $six_month = $wpdb->get_var();
        $year = $wpdb->get_var();
        //var_dump( $total_users);
        //update_user_meta( $userdata->ID, 'last_login', '' );
        //var_dump( $userdata->last_login );
        ?>
        <div class="wrap">
            <h2><?php _e( 'User Login Statistics' ); ?></h2>
 
            <table class="widefat">
                <thead>
                    <tr valign="top">
                        <th scope="col"><?php _e( 'Date' ); ?></th>
                        <th scope="col"><?php _e( 'Count' ); ?></th>
                    </tr>
                </thead>
                <?php
                if( $table ) {
                    foreach ($table as $row) {
                        ?>
                        <tr>
                            <td><?php echo $row->date; ?></td>
                            <td><?php echo $row->count; ?></td>
                        </tr>
 
                        <?php
                    } //foreach
                } else {
                    ?>
                    <tr>
                        <td colspan="7"><?php _e( 'Nothing found' ); ?></td>
                    </tr>
                <?php } ?>
            </table>
 
        </div>
 
        <?php
        $total = $wpdb->get_var( "SELECT COUNT(`id`) FROM {$this->table}" );
        $num_of_pages = ceil( $total / $limit );
        $page_links = paginate_links( array(
            'base' => add_query_arg( 'pagenum', '%#%' ),
            'format' => '',
            'prev_text' => __( '«', 'aag' ),
            'next_text' => __( '»', 'aag' ),
            'total' => $num_of_pages,
            'current' => $pagenum
                ) );
 
        if( $page_links ) {
            echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
        }
    }
 
}
 
$online_stats = new User_Login_Stats();
支付宝扫码打赏 微信打赏

如果本文对你有帮助,请打赏作者,鼓励我们继续写作!

WordPress快站:让WordPress建站更简单

关于 倡萌

一个疯狂的 WordPress 爱好者,喜欢折腾 WordPress 主题,分享 WordPress 资源。如果你也喜欢 WordPress,欢迎和我一起交流!

精彩推荐

为你的 WordPress 主题创建内置的联系表单

为你的 WordPress 主题创建内置的联系表单

修复积分插件 myCRED 日志时区相差8小时的问题

修复积分插件 myCRED 日志时区相差8小时的问题

3 条评论

  1. 测试发现 实际这个好像应该是登录后台的数量
    前台只是登录(自动登录的)好像不会计入

  2. www.nitaoba.com

    学习WordPress最好的地方。继续学习。

发表评论