當(dāng)前位置:首頁>WordPress建站>用戶交互>WordPress 統(tǒng)計每天登錄的用戶數(shù)量

WordPress 統(tǒng)計每天登錄的用戶數(shù)量

讓 WordPress 統(tǒng)計每天登錄的用戶數(shù)量,可以讓你對每天的用戶活躍程度有一個基本的了解。使用  User Login Stat 插件就可以做到這一點。它會在后臺 > 設(shè)置 > User Login Stat  顯示每天登錄的用戶數(shù)量。

user-login-stat-wpdaxue_com

在后臺插件安裝界面搜索 User Login Stat 即可在線安裝,或者下載 User Login Stat

倡萌認為將該統(tǒng)計數(shù)據(jù)放在WP儀表盤顯示會好很多,會代碼的朋友也可以參考下 WordPress 儀表盤小工具接口(Dashboard Widgets API)

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

<?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();
聲明:本站所有文章,如無特殊說明或標注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號 WPDAXUE
用戶交互

WordPress 添加前端站內(nèi)信 Cartpauj PM

2013-9-2 11:44:20

用戶交互

WordPress 限制用戶注冊可用的郵箱域名后綴

2013-9-6 7:55:00

3 條回復(fù) A文章作者 M管理員
  1. 測試發(fā)現(xiàn) 實際這個好像應(yīng)該是登錄后臺的數(shù)量
    前臺只是登錄(自動登錄的)好像不會計入

  2. chenquanwin

    發(fā)現(xiàn)了一個好地方 ??

  3. 學(xué)習(xí)WordPress最好的地方。繼續(xù)學(xué)習(xí)。

?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

北京市| 诏安县| 南丰县| 四平市| 甘肃省| 田林县| 新密市| 闸北区| 象州县| 平乐县| 江门市| 东乡县| 平定县| 黔东| 林州市| 宜章县| 禹州市| 密山市| 饶河县| 阿巴嘎旗| 新龙县| 大冶市| 永清县| 清涧县| 江安县| 永定县| 泊头市| 麻栗坡县| 合江县| 上犹县| 秀山| 巴青县| 绥中县| 青铜峡市| 射洪县| 南安市| 山西省| 贵定县| 五莲县| 定州市| 荔浦县|