Kommentare 2

Detect AdBlocker in WordPress and react properly

As you might know, I am not the biggest fan of AdBlock. While I am more worried about the potential annoying response of advertisers to more and more ad blocking users (more on this issue in German), those plugins can pull a lot of necessary money from informative websites and blogs.
Here, I want to show you how you can detect if a user is using any AdBlocker and show some specific content to him/her – all in WordPress. We cannot and will not block AdBlockers! Ads will still get blocked, but we can use the AdBlocker as some kind of switch.

Generally, there are different strategies on how to properly deal with this issue. Some news sites recently started to block their whole portal if you do not deactivate your AdBlocker (if an AdBlocker is detected, block the whole page and show a short message). This can work, because most users still want to enter those important pages. This most probably will not work for your blog or smaller news portal as you are simply not important enough. That is why I suggest the following approach: If an AdBlocker is detected, show your users a friendly message, but do not block the whole content. Alternatively, show your users another ad. AdBlockers usually do not detect affiliate-Links or simple image banners that do not use any JavaScript for tracking. You probably will not be able to track your users or use advanced ad networks, but you can still show ads (needs recurring testing). Attention: Users that use AdBlockers do not want to see any ads! This also means that they are very sensitive towards advertisement. If you show them aggressive and stupid banner ads, they will most certainly jump off at second one. Try to show them a smaller amount of advertisement that is not too prominently integrated! This way, your content AND your ads will be more successful!


Now let’s get to the tech stuff…

Valentin Allaire developed a very smart piece of code that literally generates a bait to fish for AdBlockers (more). Unfortunately, you cannot use this out of the box for your WordPress Blog – at least not if you want to stay flexible (otherwise you could just hard code it into your theme).

So, we need to write a little plugin that considers the specific functionality of the AdBlock detector and the way that AdBlockers usually work.
Don’t worry. I did this for you. However, I did not want to write another official plugin for the WordPress repository, so I just put the code and the complete plugin files here.

The idea behind the plugin: If an AdBlocker is detected, we try to show a specific message or another ad to the user. You can basically do anything, the presence of an AdBlocker acts like a switch that you can control via a simple WordPress shortcode.
It is somehow „quick and dirty“, but does its job.


First, the code

It is actually pretty easy.
First, you need the main js-file from Valentin. You can get it from his official project (see link above) or copy the version that I have used right here:


Then, we need another JavaScript that hides or shows our elements, depending on AdBlockers. It needs to be independent of the first script, in case this one gets blocked. It would be even better to totally avoid any use of the word „ad“ and hard code this file into the themes footer. However, my solution works so far and saves you a little time.


Finally, we need to combine it with the WordPress system. This means that we need to tell WordPress to load the scripts and add some shortcodes. We should also enable the possibility to make shortcodes work within widgets – if you do not want this, delete line 44.


Add those files to a new folder within your plugin-folder. You should also add a readme.txt (WordPress scheme) and an empty index.php to make the plugin work. I also recommend minifying the js-files to save resources.


Second, all the files for easy one-click-install

If you are not very familiar with writing WordPress plugins, you can simply download the plugin here and add it to your system (upload the zip-file or add the extracted folder to your plugins-folder). You will need to activate the plugin afterwards within your WordPress dashboard.

Download the files here (.zip)



Use the AdBlocker switch!

To use the plugin, simply add the following shortcodes to your pages, posts or widgets.



Der Beitrag gefällt dir? Teile ihn!
1 Comment threads
1 Thread replies
Most reacted comment
Hottest comment thread
2 Comment authors
Jens KürschnerNope Recent comment authors

This software reaches into people’s computers and takes information that they obviously would not have wanted to give. You’ve found yourself on the malware side of the coin. Mr. Rogers knew you could be a better person.