WordPress Custom AdBlock Detector
Here is a short manual how to create your own AdBlock detector. It just displays a warning message for possible blocked functionalities at the site. But it could be modified to block the entire access to the site, while an AdBlocker is in use. I've tried much similar plugins and didn't found any simple suitable solution that fits to my needs like the one described here.
The principle of the AdBlock detector is really simple – it is a script that tries to access (via post and via get methods) some URLs that look like to adds… So if the access to these URLs (files) is blocked probably (99%) there is AdBlocker. That's it.
The easiest way is to place the entire code below (except line 1) in your functions.php
file. I would prefer to create must use plugin (like it is mentioned at line 23 of the script).
<?php
/**
* Simple AdBlock Detector Script
*
* Detect AdBlockers at the user side. The URLs/URIs mentioned in the script body must be real - lines 45 to 55
* The script is compatible with Plugin:Complianz: class="cmplz-native" id='public-js-extra'.
* Refs: https://complianz.io/whitelisting-inline-script/
*
* @link https://wiki.szs.space/wiki/WordPress_Custom_AdBlock_Detector
* @version 0.0.1
*
* @wordpress-plugin
*
* Plugin Name: Simple AdBlock Detector Script
* Plugin URI: https://wiki.szs.space/wiki/WordPress_Custom_AdBlock_Detector
* Description: Detect AdBlockers at the user side.
* Author: Spas Z. Spasov
* License: Cc-by-4.0
* License URI: https://wiki.szs.space/wiki/Template:Cc-by-4.0
* Text Domain: (not defined in this version)
* Domain Path: (/languages/)
*
* Deployment path and file name: wp-content/mu-plugins/wp-ad-block-detector.php
**/
function simple_AdBlockDetector_javascript() {
?>
<script class="cmplz-native" type="text/javascript" id='public-js-extra'>
jQuery(document).ready(function(){
function onGetScriptFail() {
console.log('AdBlock status: Blocked!');
if ($('div#adBlockDt-legal-note').length === 0) {
$("head").append('<style>#adBlockDt-legal-note{ background-color: white; font-size: 16px; line-height: 1.4; padding: 8px 16px; height: auto; display: flex; font-family: \'Segoe UI\', \'IBM Plex Sans\', sans-serif; cursor: pointer; }</style>');
var legalNote = 'The message amout the AdBlocker that will be displayed!'
$('div.main-container').prepend('<div id="adBlockDt-legal-note">'+ legalNote +'</div>');
$('div#adBlockDt-legal-note').on('click', function() { $(this).delay(200).hide(1800); });
$('div#itfes-legal-note').hover( function () { $(this).delay(1200).hide(1800); });
}
}
$.getScript(
"/wp-content/plugins/add-to-any/addtoany.min.js",
"/wp-content/plugins/complianz-gdpr-premium/assets/js/cookieconsent.min.js",
"/wp-content/plugins/complianz-gdpr/assets/js/cookieconsent.min.js",
"https://static.addtoany.com/menu/page.js",
"https://www.google-analytics.com/analytics.js"
)
.done(function( data, status ) { console.log('AdBlock GET status: ' + status + '.'); })
.fail(function( jqxhr, settings, exception ) { onGetScriptFail(); });
$.post(
"https://stats.g.doubleclick.net/j/collect"
)
.done(function( data, status ) { console.log('AdBlock POST status: ' + status + '.'); })
.fail(function( jqxhr, settings, exception ) { onGetScriptFail(); });
});
</script>
<?php
}
add_action('wp_head', 'simple_AdBlockDetector_javascript');