Posted on Leave a comment

How to Exclude Your Activity From Your Site’s Code and Analytics

Black and white dog with disguise eyeglasses

Last updated on March 28th, 2020

The Problem

Sometimes, when working on a WordPress site, you want to exclude your own activity, either from executing code or from analytics tools connected to your site (such as Google Analytics), or both. There are a few possible methods.



The most basic solution is using the current_user_can('administrator') function for conditionally executing code blocks, or disabling analytics tools. Unlike the is_admin() function, it runs also on the frontend, not just on the admin dashboard. So, as long as you visit your site’s frontend logged in as an admin — you’re covered. The thing is, you might sometimes visit your own site not logged in as an admin…

Ad Blockers

As for excluding yourself from analytics tools, there is a great solution that is not dependent on your logged-in status. It also comes with a benefit — adblocking. There are many ad blockers out there. AdGaurd is a great one. It will prevent you from being tracked by analytics tools for free both on desktop and mobile. It has a chrome extension, a desktop app, and a mobile app.

When installing one of the AdGuard solutions, make sure to apply the “AdGuard Tracking Protection filter” privacy filter — that will keep you hidden from analytics tools.

If you’re using Android, keep in mind that on Google Play you will only find the AdGuard extension for Samsung and Yandex browsers. Google removed the actual AdGuard mobile app from their app store. You can get it by visiting the AdGaurd site, downloading it and manually installing it.

This is a great solution, but only for excluding yourself from analytics tools. Sometimes you may want to exclude yourself from specific code on your site, even while not logged in as an admin. Luckily, there is a way:


In this site’s hobbies section, I wanted to be able to order hobbies by their popularity. The site is built on WooCommerce, which has a built-in popularity mechanism and sorting option — but it only counts purchases, and nothing is being purchased on my site. I needed to redefine popularity as view count, so I created a dedicated custom field and wrote a code to increase the counter on each hobby page visit. But, as currently, I myself am the devotest visitor on my site — my activity will distort the count data. The current_user_can('administrator') function could have helped here, but many times I’m visiting and testing the site while logged out.

This is the logic of the solution I came up with:

  1. When you visit the admin dashboard — get your IP.
  2. Save your IP as an admin IP.
  3. Before executing a certain block of code, check if the current user’s IP matches the saved admin IP.

Now, the code:

(1 + 2) — Get your IP and save as an admin IP

1st code block explained:

By using the admin_head action hook we only execute the function on an admin dashboard visit. Then, we are using the $_SERVER['REMOTE_ADDR'] PHP super-global variable to get the current user’s IP (mine, or yours), and assign it to $current_admin_ip. We then save the IP in a transient to be able to access it later, in other sessions. The if statement checks if saving needed to be done. If the transient is already set and equal to the current admin IP — no saving needed. IPs are changing from time to time, that’s why we are checking the IP on each admin dashboard visit.

(3) — Check if the current user’s IP matches the saved admin IP

2nd code block explained:

This function compares the current $_SERVER['REMOTE_ADDR'] — meaning, the current visitor’s IP — with the saved admin IP transient. It returns true if they are equal, and false otherwise. The use of a static variable makes sure that the body of the function is only being executed once per page. On the second time, $is_admin_ip is already set and returned immediately.

This function can then be used everywhere. On my case, for example:

If the current user uses the admin’s IP — don’t increase the view count.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.