Anti-adblock that replaces div [duplicate]
I need a wordpress plugin or script in which appreciate a message to the user who has adblock.
However, I want this message to appear in place of a div.
Already tried the Plugins:
JGC AdBlocker Detector - this one does not accept style (shows the style as if it were plain text)
Ad Blocker Notify Lite - this works on the local server, but does not works on the online server.
2 answers
You can use the BlockAdBlock (or FuckAdBlock)
As I answered in: https://pt.stackoverflow.com/a/87992/3635
If you have jQuery (usually wordpress uses) you can do like this:
<script src="blockAdBlock.js"></script>
<script>
(function() {
function adBlockDetected() {
$(function () {
$('#div_especifica').text("adblock detectado");
});
}
function adBlockNotDetected() {
console.log("Sem adblock");
}
if(typeof blockAdBlock=== 'undefined') {
alert("blockAdBlock não foi carregado");
} else {
blockAdBlock.onDetected(adBlockDetected);
blockAdBlock.onNotDetected(adBlockNotDetected);
blockAdBlock.on(true, adBlockDetected);
blockAdBlock.on(false, adBlockNotDetected);
blockAdBlock.on(true, adBlockDetected).onNotDetected(adBlockNotDetected);
}
blockAdBlock.setOption('checkOnLoad', false);
blockAdBlock.setOption({
debug: true,
checkOnLoad: false,
resetOnEnd: false
});
})();
</script>
Assuming the element is something like:
<div id="div_especifica"></div>
Alternative
If you are using googleanalitycs you could use like this:
<script>
function possivelAdblockDetectado () {
$(function () {
$('#div_especifica').text("adblock detectado");
});
}
</script>
<script onerror="possivelAdblockDetectado()" async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
style="display:inline-block;width:300px;height:250px"
data-ad-client="ca-pub-xxxxxxxxxxxxxxxx"
data-ad-slot="6440411535"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
There are several ways to detect ad blocks, but none is 100% guaranteed, I tested a script
taken from an answer from @ Guilherme Nascimento: https://pt.stackoverflow.com/a/87992/3635
<script>
function possivelAdblockDetectado () {
document.getElementById("id-div").innerHTML = "Conteúdo html que você quer mostrar no lugar";
}
</script>
<script onerror="possivelAdblockDetectado()" async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
This script
tries to load Google ADS, if it is not possible, due to ad blocking (or any other reason), it will call a function possivelAdblockDetectado ()
, and in it you can exchange the content of the div
you want.
Hope I helped!