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.
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:
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.
Use the AdBlocker switch!
To use the plugin, simply add the following shortcodes to your pages, posts or widgets.