WordPress – How to display message to adblocker users

WordPress-AdblockMessage1

The following WordPress script can be used to display a message to users that are running adblocker software.

The script has been written for websites running Google Adsense advertisements and simply adds a message where the advertisements would have been displayed.

To install the code, add it to your theme’s functions.php file (anywhere after the opening <?php line).

Why use this code?

Advertisements play an important role in the internet eco-system. Without advertising many websites would not be able to fund their operational costs such as hosting, domain name and software such as your theme, plugins and any special developments.

I believe that people are drawn towards using adblockers because advertisements are becoming more annoying – advertisements that stop you from viewing content – for example popup ads or minute long video ads that stop you from watching your favourite cat video on YouTube or having too many or inappropriately placed advertisements on a web page.

Unlike the handful of adblocker notify plugins in the WordPress plugin directory this code is PASSIVE this code will not annoy or prevent access to the web page content. It simply informs the visitor of the importance of advertisements and asks them to whitelist your website in their adblocker.

The message is also dismissable – so it can be closed and never seen again by the user on the their browser (this is done using a cookie).

Does this voilate AdSense policies?

No. I have spoken with the AdSense team and confirmed that doing this does not voilate AdSense policies.

The important things to note here is that we’re not modifying the AdSense code, we’re not providing any ‘false clicks’ and we’re not providing any ‘false impressions’.

Having troubles installing this code?

Download and install the plugin version of this code.

(note this is NOT offered through the WordPress plugin directory — updates cannot be provided automatically)

Download: https://www.itsupportguides.com/downloads/itsg-adblocker-prompt.zip

Code

add_action('wp_footer', 'itsg_adblock_message');

function itsg_adblock_message() {
    ?>
    <script>
        jQuery(document).ready(function() {
            
            var dismiss_cookie_name = 'itsg_adblock_message_dismissed';
            var dismiss_cookie = dismiss_cookie_name + '=1; Max-age=315360000; path=/'; // 10 years (60*60*24*365*10)
            setTimeout(function(){
                if (jQuery('.adsbygoogle').height() == 0 && document.cookie.indexOf(dismiss_cookie_name) == -1) {
                    jQuery('.adsbygoogle').css("display", "block");
                    jQuery('.adsbygoogle').after("<div class='adblock_wrapper'><span class='adblock_wrapper_close'>Close</span><h2>Adblocker detected</h2><p>It looks like you're using an adblocker to prevent advertisements from displaying.</p><p>This website uses advertisements to pay for hosting and other operational costs.</p><p><strong>We ask that you whitelist <?php echo $_SERVER['SERVER_NAME'] ?> in your adblocker to help keep the website running. Thanks!</strong></p></div>");
                }
                var disclaimer_close = jQuery( '.adblock_wrapper_close' );
                disclaimer_close.click( function() {
                    jQuery( '.adblock_wrapper' ).hide();
                    document.cookie = dismiss_cookie;
                })
            },2000);
            
    
});
    </script>
    <style>                
        .adblock_wrapper {
            border: 3px dashed red;
            padding: 5%;
            text-align: center;
        }
        
        .adblock_wrapper_close {
            float: right;
            cursor: pointer;
            font-weight: 800;
        }
        
        .adblock_wrapper_close::after {
            content: "X";
            margin-left: 10px;
        }
    </style>
    <?php 
}