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.

15 thoughts on “Detect Adblock Users on a WordPress Blog”

  1. You’re great! So simple and so functional.

    Because I don’t want adblocking users at all (you know… ads is the only way to pay the webhosting bills), I made a simple change and I’m redirecting the user at adblockplus.org.

    My simple act is saying “Take back the user. I don’t want to consume my precious bandwidth and system resources.” 😉

  2. Some people don’t know there are many ways to get rid of ads (and it’s stupid forcing people watching them, if they won’t click on).

    I’m talking about Stylish. It’s not as easy as set up Adblock, but effective.

    And also you can get rid of captcha using another approach against spammers. Javascript and cookies can help filtering, since most robots don’t handle both.

    Have fun and good luck 🙂

  3. thank you for the information. gonna implement it at my blog. i dont have much advertisments on my blog but the ones i do have i dont want them to be blocked.

  4. I am using wordpress with adsense plugin. So I have to enter adsense code on the plugin setting page and that’s it. Now I have a problem to install your code!

    “Add this just before the closing body “” tag:”

    There are no body tag to see in wordpress! so where can I put last code?

    need ur help!

  5. Guy .. Excellent .. Amazing .. I will bookmark your website and take the feeds alsoI’m satisfied to seek out a lot of helpful information here within the post, we need develop more strategies in this regard, thank you for sharing. . . . . .

  6. Thanks for that idea, thanks! Exactly, what i was looking for – its not that i have to harras the users, but it would be cool if i could get 3 dollars for the hosting stuff 🙂

  7. Hi!
    i was testing your Script but cant get it to work with 3.5… do i have to implement the script in the functions.php? Its looks so simple and easy its exactly what i am needing 🙂

  8. I see your site needs some unique content. Writing manually
    is time consuming, but there is solution for this hard task.
    Just search for – Miftolo’s tools rewriter

Leave a Reply

Your email address will not be published.