修改WP-PostViews插件实现统计除管理员外的所有人

发表评论 阅读评论

WP-PostViews能够记录文章和页面被访问的次数,然后进行统计,功能很强大。
但是统计选项只有3个: 所有人、仅游客、仅注册者。
现在修改WP-PosetViews使其支持统计除管理员外的所有人。

1.修改postviews-options.php文件

在{wordpress}wp-contentpluginswp-postviews目录里找到postviews-options.php文件。
搜索 <select name="views_count" ,在最后一个option后面添加下面的代码:(大约在148行)

<option value="3" <?php selected('3', $views_options['count']); ?>><?php _e('Everyone Not Site Admin', 'wp-postviews'); ?></option>

修改后的代码为:

<select name="views_count" size="1">
    <option value="0"<?php selected('0', $views_options['count']); ?>><?php _e('Everyone', 'wp-postviews'); ?></option>
    <option value="1"<?php selected('1', $views_options['count']); ?>><?php _e('Guests Only', 'wp-postviews'); ?></option>
    <option value="2"<?php selected('2', $views_options['count']); ?>><?php _e('Registered Users Only', 'wp-postviews'); ?></option>
    <option value="3"<?php selected('3', $views_options['count']); ?>><?php _e('Everyone Not Site Admin', 'wp-postviews'); ?></option>
</select>

2.修改 wp-postviews.php文件

在{wordpress}wp-contentpluginswp-postviews目录里找到wp-postviewss.php文件。
搜索 function process_postviews(),在switch的最后一个case后面添加下面的代码:(大约在83行)

case 3:
    if(!is_super_admin()) {
        $should_count = true;
    }
    break;

修改后的代码为:

function process_postviews() {
    global $user_ID, $post;
    if(!wp_is_post_revision($post)) {
        if(is_single() || is_page()) {
            $id = intval($post->ID);
            $views_options = get_option('views_options');
            $post_views = get_post_custom($id);
            $post_views = intval($post_views['views'][0]);
            $should_count = false;
            switch(intval($views_options['count'])) {
                case 0:
                    $should_count = true;
                    break;
                case 1:
                    if(empty($_COOKIE[USER_COOKIE]) && intval($user_ID) == 0) {
                        $should_count = true;
                    }
                    break;
                case 2:
                    if(intval($user_ID) > 0) {
                        $should_count = true;
                    }
                    break;
                case 3:
                    if(!is_super_admin()) {
                        $should_count = true;
                    }
                    break;
            }
            if(intval($views_options['exclude_bots']) == 1) {
                $bots = array('Google Bot' => 'googlebot', 'Google Bot' => 'google', 'MSN' => 'msnbot', 'Alex' => 'ia_archiver', 'Lycos' => 'lycos', 'Ask Jeeves' => 'jeeves', 'Altavista' => 'scooter', 'AllTheWeb' => 'fast-webcrawler', 'Inktomi' => 'slurp@inktomi', 'Turnitin.com' => 'turnitinbot', 'Technorati' => 'technorati', 'Yahoo' => 'yahoo', 'Findexa' => 'findexa', 'NextLinks' => 'findlinks', 'Gais' => 'gaisbo', 'WiseNut' => 'zyborg', 'WhoisSource' => 'surveybot', 'Bloglines' => 'bloglines', 'BlogSearch' => 'blogsearch', 'PubSub' => 'pubsub', 'Syndic8' => 'syndic8', 'RadioUserland' => 'userland', 'Gigabot' => 'gigabot', 'Become.com' => 'become.com');
                $useragent = $_SERVER['HTTP_USER_AGENT'];
                foreach ($bots as $name => $lookfor) { 
                    if (stristr($useragent, $lookfor) !== false) { 
                        $should_count = false;
                        break;
                    } 
                }
            }
            if($should_count) {
                if(defined('WP_CACHE') && WP_CACHE) {
                    echo "n".'<!-- Start Of Script Generated By WP-PostViews 1.50 -->'."n";
                    wp_print_scripts('jquery');
                    echo '<script type="text/javascript">'."n";
                    echo '/* <![CDATA[ */'."n";
                    echo "jQuery.ajax({type:'GET',url:'".plugins_url('wp-postviews/wp-postviews.php')."',data:'postviews_id=".$id."',cache:false});";
                    echo '/* ]]> */'."n";
                    echo '</script>'."n";
                    echo '<!-- End Of Script Generated By WP-PostViews 1.50 -->'."n";
                } else {
                    if(!update_post_meta($id, 'views', ($post_views+1))) {
                        add_post_meta($id, 'views', 1, true);
                    }
                }
            }
        }
    }
}

3.最后到后台修改Count Views From:选项为 Everyone Not Site Admin

postViewsOptionsForAdmin.jpg

这里有修改好的文件下载 wp-postviews.zip
直接解压到{wordpress}wp-contentplugins目录就可以了(不必再去下载WP-PostViews插件了)

  1. 何苦呢 | | #1

    刚用上这个插件

  1. 本文目前尚无任何 trackbacks 和 pingbacks.
回到顶部