After I published the adblock statistics plugin BlockAlyzer for WordPress, some readers and WordPress users asked if this can be used to interact with adblock users, like showing a message or forwarding them to another page. Even though, the intention of the plugin lays on adblock statistics, you are able to use the adblock detection in the plugin for your own purposes. This article will describe how to implement anti adblock strategies with JavaScript or PHP.

BlockAlyzer

To use the solution described here you need to install BlockAlyzer in your WordPress blog. Visit the BlockAlyzer adblock counter main page to learn more about the plugin or directly install it from wordpress.org.

You're using WordPress and would like to get Advanced Ads for free?

Send adblock users to another page

There are a lot of anti adblock stategies out there. Some are as nice as a small message to switch off your adblock plugin. Others are more radical and prevent you from reading content or forward adblock users to another site. I personally respect adblock users and would first think about the friendly message telling them how the usage of adblock reflects on my sites content by limiting the ressources I have to build them.

I was directly asked a multiple times how to forward an adblock user to another website. So, despite me never using this on my sites, I choose this example to show you how BlockAlyzer can help you to do almost anything after detecting an adblock user. Be also advised, that I don’t know what effects this might have for the Google bot and on your ranking.

Adblock action with JavaScript

The adblock test is based on JavaScript, so reacting on adblock with JavaScript is the most reliable you can do. When adblock is detected the variable ba_blocked is set to true. It’s default value is false.

So the most simple working code example would be the following:

<script type="text/javascript">
if ( ba_blocked == true ) alert("You are using adblock.")
</script>

You can include this code into your website via the wp_footer-action, in a new js-file or just the line inbetween the <script> tags in an existing JavaScript code block that is used after the BlockAlyzer code in the footer.

UPDATE: BlockAlyzer uses a small timeout before checking for adblocker, so this also needs to be included in the code. See this gist for a full example of code you can include into your functions.php.

Forward adblock user with JavaScript

To redirect an adblock user to a different external page, just use this snippet.

<script type="text/javascript">
if ( ba_blocked == true ) window.location = "http://www.example.com/";
</script>

This will send visitors with activated adblock to http://www.example.com/. Please don’t use this code to forward to an internal page of the same website. It might create an infinite loop.

Adblock action with PHP

The other possible way BlockAlyzer offers you to know if a visitor uses an ad filter on your site is with a constant set in PHP. This constant is called BA_ADBLOCK_ENABLED.

The most easy check is this one.

if ( defined('BA_ADBLOCK_ENABLED') && BA_ADBLOCK_ENABLED == true )<br />
echo "You are using adblock.";

You can use this code in a template file or the functions.php of your theme. Be aware that this might not work in plugins, if they are loaded before BlockAlyzer. See below for a solution for that.

Please also notice that I added a check for the constant. This might be helpful if you decide to disable the plugin one time and don’t want ugly error messages to be displayed.

Currently, there are two dilemmas with the PHP constant. Since the check of adblock is done and can only be done with JavaScript, the PHP constant can only be true after the first page view. So on the first page a user visits the constant is false even if he uses an ad filter. At the other end, if a user switches off adblock and doesn’t clear the cookies the constant might still be true if he returns to your site. This might be fine with a simple message, but might cause problems with the following snippet for a site redirect (if it is not the same website).

Redirect adblock user with PHP code

To redirect a user with adblock in PHP this code needs to be implemented in the functions.php file of your theme and not any other template file, because the header() function cannot be called after the first output was sent to the browser.

if ( defined('BA_ADBLOCK_ENABLED') && BA_ADBLOCK_ENABLED == true )<br />
header( 'Location: http://www.example.com/' );

Like with the JavaScript code, don’t use this with a location that is on your site without first checking, if you might already be on it. Else you would create an infinite loop.

Check with Cookie before the plugin is loaded

To set the PHP constant, the JavaScript code sets a cookie that is checked on the next page view when setting the BA_ADBLOCK_ENABLED constant. So if you need the information on adblock users before the plugin is loaded or even in your own plugin, you might check the cookie information like this.

if ($_COOKIE['BaAdBlock'] === 'enabled') die('You are using adblock.');

When adblock wasn’t detected, the cookie is set to “disabled”.

This is the basic implementation for an anti adblock stategy in WordPress. I am planing on developing such a feature in BlockAlyzer or as an additional plugin. Please subscribe to the newsletter if you are interested or write a comment to motivate me on this.

The Author

Comments

  1. Nico

    Hi, i want to test this, how would i go about displaying an external .php file or html file to display as popup?

    Thanks!

    1. Thomas Maier Article Author

      Hi Nico,
      since this test happens in javascript in your browser it is not possible to call a php file directly. You can do this using Ajax or you can hide your popup in your code and just show it when the adblock test is possitive.
      Hope this helps
      Thomas

  2. Nico

    Oh nvm i missed the part that said not to redirect to an internal page. i just want them to either disable adblock or leave. i don’t care having less visitors they are eating my bandwidth i just want to keep the users that are whiling to accept the site ads and block the rest.

    1. bishop

      You wish 😀 Apps like Adguard or Ad Muncher will block all possible ads (users can create custom filters). So there’s no merit in doing this. ^^

      1. Thomas Maier Article Author

        You are right, in most ad blockers, users can create their own filters, but most people don’t use that. Anyway, don’t just think about ads here. Webmasters can use the free space for other useful things as well.
        Thomas

  3. keith

    Hey Thomas,

    I’ve been trying to get this to work for the last 2 days now and it still haven’t worked. I’m using optimizepress as my theme and I added the javascript code above where the rest of my javascript codes are at in optimizepress but nothing. All the other javascript codes work, except for this one. I have a content lock site, and adblocker plus disables my content locker (Blackhat Codebreaker) and I’m trying to redirect adblock users to another page so the content that’s locked, won’t get downloaded without first having a offer filled out. I already have BlockAlyzer installed on my site.

    Can you please help. Do you have a video that shows exactly how to implement this? Or can you explain it better. Like do I have to go into the file manager of my hosting to make this work or what? I’m at a dead end and any info will help.

    1. Thomas Maier Article Author

      Hey keith,
      did you choose a javascript-only approach? Then please send me the link to the website and I will have a look into the code.
      Thomas

  4. keith

    Oh yeah, can you please add this feature to BlockAlyzer. It would make things alot easier if you did. BTW, love the plugin, I have it installed on all my sites 🙂

  5. John

    I have tried to apply the javascript and PHP solutions you have presented here in a test file with no luck. I do not believe these work any more. I have Google Chrome with Adblock and Adblock Plus. I’ve tried toogling it, tweaking the code, etc. I’ve been coding PHP nearly since it came out, so I’m pretty good with that. Can you verify that this still works, and provide a single file (ads included) that I can run and try to make it work?

    1. Thomas Maier Article Author

      Hi John, thanks for reporting this. Are you using this somewhere live where I can have a look?

  6. Noypi

    Hello,

    I have installed your plugin on my website a while ago and added the code from https://gist.github.com/webzunft/9078689 on my functions.php and it’s working good. My question is how do I add a countdown timer so the visitors have to wait for a certain time to be able to close the popup. Thank you and sorry for my English.

    1. Thomas Maier Article Author

      Hi Noypi, thanks for giving the feedback that it works! I am sorry, but I am not able to provide code for your task out of the box. You can contact me via the contact form if you need a coder to implement this.

  7. Benjamin Michael Thomas

    Hello Thomas Maier,

    Thank you for very useful BlockAlyzer plugin and the advise here. Unfortunately I am running into issues making the above work.

    I just want to display a simple message in the site header (not a pop alarm) for Adblock users. However, not matter if Adblock extension is enabled or disabled it always displays the message:
    http://dev.designmadeinjapan.com/

    This is the JS I am using:

    jQuery(document).ready(function($) {
    setTimeout(function(){
    if ( ba_blocked == true ) {

    $(“.fix_header”).append(“Please switch off your adblocker. Ads are the only source of income for DMIJ. Thank you!”);
    }

    },200)
    });

    What am I doing wrong? Thanks for your advise.

    1. Thomas Maier Article Author

      Hi Ben, works perfectly for me when visiting your site using Firefox with and without AdBlock enabled.

      1. Benjamin Michael Thomas

        Thanks for your reply. True, for some reason Firefox seems to get it right.

        However with Chrome and Opera the Adblock notification is show not matter what (Adblock extension enable or disabled). Is the JS I am using not compatible with Chrome? Can you confirm the error on Chrome?

        1. Thomas Maier Article Author

          Hi Ben, looks strange in Chrome, because your code seems to be outcommented, but the message is shown anyway. I probably missed something here.
          Anyway, please try 2 things (in this order).
          1. move your code below the code added by BlockAlyzer (into the footer).
          2. raise the timeout from 200ms to maybe 500.

          1. Benjamin Michael Thomas

            Hi Thomas,

            Many thanks for the tips, it seems to work mostly reliable now.
            Just sometimes when doing a hard browser refresh (with deactivated Adbocker) it still shows the message for some reason. In any case, this is a great and I have deployed this function on the live site now.

            I hope it will decrease the adblocker rate a little bit on the site. I think most users do no even think about selectively whitelisting certain sites and are not really aware what adblocking does as a whole to the online publishing economy.

        2. Thomas Maier Article Author

          I am glad it worked. If I may ask, how high is your AdBlocker rate?

  8. James

    Hi Thomas, I like the way your plugin works for WordPress. I put together a similar script that works on non-WP pages and published the code at http://scriptchecked.com/

    ScriptChecked was originally put together to help protect user a/cs at http://upvoting.com/

    By the way, I found your blog when searching for Adsense revenue. The Upvoting.com site offers revenue sharing via Adsense (create a topic and we share ads with your a/c) and we have just dipped our toes into the world of WordPress plugins by releasing a plugin that allows bloggers to feature their topics in their sidebar and posts. Developer info at http://upvoting.net/

  9. Pingback: [Newbie] 11k uniques/day - virtually no income

  10. Ben

    Hi Thomas,

    I’ve used your plugin and it shows something like 90% of my users have adblock. Such a high figure i guess.

    When i tried with another service called PageFair, it shows around 2% adblock.

    Please advice why such a vast difference.

    Thanks.

    1. Thomas Maier Article Author

      Hi Ben, both numbers seem odd. I guess BlockAlyzer has problems measuring on about 1/10 of websites, but haven’t yet figured out the cause. Maybe your site is one of those and PageFair lacks from the same problem.

  11. Devon

    Hi, I added this code at the bottom of my functions.php file:

    function adblock_alert(){
    ?>
    jQuery(document).ready(function($) {
    setTimeout(function(){
    if ( ba_blocked == true ) alert(“It is detected that you are using AdBlock. T3CHSMASH would appreciate if you could kindly choose to not run AdBlock on this domain. Thanks in advance.”);
    },10000)
    });
    <?php
    }
    add_action('wp_footer', 'adblock_alert');

    However, the message shows up whether or not adblock is enabled. Any suggestions?

    1. Thomas Maier Article Author

      Hi Devon, I can’t see a problem in the code and also don’t have problems when visiting your site with or without AdBlock enabled.

  12. Pingback: Artikel kostenpflichtig anbieten - So gehst du vor - Rico Marketing

  13. didla08

    Hi Thom,
    Can you explain me to use anti-adblock script with costume image message? And visitor can close it in costume time.
    I look many website can do that.
    Please help.

    Thanks.

    1. Thomas Maier Article Author

      Hi, this is a great idea and shouldn’t be too hard to implement, but even I don’t just pull this code out of my pocket 🙂 I am hoping to get back to the adblocker topic soon and will share my results and maybe even some code I use.

      1. didla08

        Thanks for reply sir.
        I search with google and find it in antiblock[dot]org and change some html with scr image and that’s work.
        But i see in game-debate[dot]com is more attractive anti-adblock message. Do you know about that html sir?

        Thanks..

        1. Thomas Maier Article Author

          no, sorry, I currently don’t have an html script other than the ones presented here.

  14. Sergey Pimenov

    Hi all

    if ( ba_blocked == true ) … not work on my site

    To check Adguard i’m use this code:

    $(function(){
    if (window.AG_onLoad != undefined) {
    var div = $(“”).css(‘…you block style…’).html(“Support the project! Please switch off your adblocker.”).insertAfter(‘header’);
    }
    });

  15. Amar

    Will it work on all adblocks ? and will it detect adsene ads?
    ba_blocked is pre-defined?
    Let me know
    Thanks

    1. Thomas Maier Article Author

      I haven’t tested all kinds of ad blockers out there, so I can’t really answer the first question. The script does not detect AdSense or any other ads, it is to detect ad blockers. If I remember correctly, ba_blocked is pre-defined.
      Thanks, Thomas