WordPress 統計每天登錄的用戶數量

讓 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();

<?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();

You May Also Like