How to implement anti adblock strategies in WordPress

Software
By: Thomas Maier

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.

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 ) {
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 ) {
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

Leave a Reply

Your email address will not be published. Required fields are marked *