Don’t Count Administrators In WordPress.com Stats Plugin
The WordPress.com Stats plugin is great for tracking page-views of your visitors. You can see the top posts and pages, the incoming referral links and search terms, and the outbound links to where your visitors left. As the Administrator, you have two options available for modification. You can choose to:
- show the site stats to users of any role, like Editors, Contributors, and Subscribers
- enable/disable stats counting of logged in users
There is something missing that I wish was an addition to the latter setting – the ability to disable stat counting for specific users or roles while keeping it enabled for the rest.
Let me explain my scenario. I have a main Administrator account that has all the WordPress settings available, and I have an everyday account that I use to moderate the site, view stats, and make posts like this one. People do register accounts and I plan on making greater use of registered users in the future. I want to count the page-views of all visitors, but I don’t want to count my personal page-views. I’m the site administrator and I visit many pages regularly throughout the site for bug testing and whatnot. I don’t want my visits to skew the stats. A visitor stats plugin should track just visitors, not site administrators.
Doing what ended up being a small adjustment to the WordPress.com Stats plugin, I was able to block counting of the main Administrator account and my everyday account, but still count everyone else, whether they were logged in or not.
Open /wp-content/plugins/stats/stats.php
and locate line 194:
global $wp_the_query, $current_user;
In the blank line after, add this:
if ( ! current_user_can('administrator') && ! current_user_can('editor') ) {
The logic statement here says, If the current user is Not of the Administrator role, and If the current user is Not of the Editor role, continue with the stats_footer() function.
On line 226, there is a single right-curly-brace. Add a second one to close the If statement we just created. Lines 225-228 should now look like this:
<?php
}}
function stats_array($kvs) {
As you can tell from the code, my everyday account is of the role Editor. If you only use an Administrator account, you can adjust the code I added in line 195 to remove the check to see whether the current user is not an Editor. Line 195 would now look like so:
if ( ! current_user_can('administrator') ) {
Keep in mind that you will need to make this adjustment every time the plugin is updated, so it may be a good idea to bookmark this page.
Editor’s note: This article has been updated for the most recent version of this plugin: WordPress.com Stats version 1.8.5. Instead of locating the code on line 90, you’ll find it on line 194. Add the right-curly-brace on line 226 rather than line 122.