WordPress Custom AdBlock Detector

From WikiMLT
Revision as of 19:28, 17 July 2022 by Spas (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Here is a short man­u­al how to cre­ate your own Ad­Block de­tec­tor. It  just dis­plays a warn­ing mes­sage for pos­si­ble blocked func­tion­al­i­ties at the site. But it could be mod­i­fied to block the en­tire ac­cess to the site, while an Ad­Block­er is in use. I've tried much sim­i­lar plu­g­ins and didn't found any sim­ple suit­able so­lu­tion that fits to my needs like the one de­scribed here.

The prin­ci­ple of the Ad­Block de­tec­tor is re­al­ly sim­ple – it is a script that tries to ac­cess (via post and via get meth­ods) some URLs that look like to adds… So if the ac­cess to these URLs (files) is blocked prob­a­bly (99%) there is Ad­Block­er. That's it.

The eas­i­est way is to place the en­tire code be­low (ex­cept line 1) in your functions.php file. I would pre­fer to cre­ate must use plu­g­in (like it is men­tioned 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');