Detect Adblock Users on a WordPress Blog

The following code allows you to display an alternate message for your advertisements to users who are using Adblock as well as most other modern online advertisement blocking applications.
adblockplus
In your blog’s root directory (folder containing the wp-addon, wp-content and wp-includes sub folders), create a file and name it “adframe.js”.

This name is crucial because most ad blockers will prevent files with this name from being accessed by the browser.  In the file you have just created insert the following code:

adblock = false;

Now go to where you have added the advertisements in your code.  For instance, if you’re running Google Ads, it would look something like this:

<script type="text/javascript"><!--
google_ad_client = "pub-xxxxxxxxxxxxxxxxx";

Just before “<script” add the following div tag:

<div class=”a”>

After the second “</script>” tag, add the closing div tag “</div>”

The last thing to do is add the javascript logic that decides if we should display the alternate message.  Add this just before the closing body “</body>” tag:

<script type="text/javascript">
<!--
 var adblock = true;
//-->
</script>
<script type="text/javascript" src="http://<your-blog-domain>/blog/adframe.js"></script>
<script type="text/javascript">
<!--
 if(adblock) {
 var allElements = document.getElementsByTagName('div');
 for (var i = 0; i < allElements.length; i++) {
 if (allElements[i].className == 'a') {
 allElements[i].innerHTML = 'Hello, you are blocking ads.';
 }
 }
 }
</script>

Be sure to replace “<your-blog-domain>” with your own as appropriate.

Users running ablocking software will see the following message ‘Hello, you are blocking ads’.  Of course you can substitute this message with anything you want, or even add some html.  I should note that this is a prickly issue with valid points for and against ‘blocking the blockers’.  I found an excellent example of the central arguement anonymous user “Headz” posted on another site.

>>It’s their connection. Their experience.

“You’re not entitled to your own person experience when visiting a website, anymore than when visiting a McDonald’s, or your local bank. If you walk into a McDonald’s, and there’s a huge annoying neon sign promoting some burger, you don’t have the right to tear it down to better accommodate your dining experience. It’s the restaurant owner’s prerogative to structure his business how he or she chooses, which may include big annoying signs. If you don’t like it, you have other dining options — Like the Burger King across the street.

If you don’t like that your favorite tech site is flooded with ads, then go somewhere else. You have other tech site options. It’s arrogant to think you have a right to tear someone’s business apart to better accommodate your personal experience. You wouldn’t do it at a McDonald’s, so I don’t understand how you feel justified doing it at a website.”

Of course, his argument fails to consider the differences between brick-and-mortars and electronic venues.  I’m not sure which side I’m on for this one, I’ve already flip-flopped twice.  Thanks to http://detect-ad-blocking-software.webconrad.com/ for the original code snippet.