Don’t Count Administrators In Stats Plugin

By .     1 Comment

The 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:

  1. show the site stats to users of any role, like Editors, Contributors, and Subscribers
  2. 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 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.

Posted in: Web Development

is the site owner of Computer Tech Tips and is passionate about computer technology, particularly Windows-based software, malware removal, and web development. He enjoys helping people troubleshoot computer problems and providing technical support to family, friends, and people around the net. Xps wrote 79 article(s) for Computer Tech Tips.

 Subscribe to comments: this article | all articles

Comments (1)

  1. XPS says:

    Editor’s note: This article has been updated for the most recent version of this plugin: 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.

Comments are automatically closed to prevent SPAM.