Block Adsense on specific page

Set-up

I have the following standard Adsense script inserted in my header.php file,

script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"
/script
script
    (adsbygoogle = window.adsbygoogle || []).push({
    google_ad_client: "ca-pub-********",
    enable_page_level_ads: true
});
/script

This allows me to display ads on all pages instantly without too much hassle.


Problem

I'd like to prevent display of all adsense ads on specific pages of my website.

I'm looking for some 'if' function to stop the display. Anyone knows of such a function?

Topic adsense Wordpress

Category Web


Step 1: Login to your your AdSense dashboard and click on My ads>>Auto ads>>New URL group

Step 2: Select URLs The next thing you need to do is to enter all the URLs you want to choose a new global setting for…click on ‘Add URL’.

Step 3: Select ad settings for new URL groups Since you don’t want ads displaying on those pages, toggle off all ads format for those pages and click on ‘next‘.

Source: https://www.naijahomebased.com/stop-google-ads-from-displaying/


I can recommend the free plugin Advanced Ads for this mission. You can easily select the pages on which you want to (not) inject this code. Installation and setup will take around 10 minutes, but afterward, you can add pages without any need to touch code.

Here is a tutorial about this setup with Advanced Ads: https://wpadvancedads.com/how-to-block-ads-on-a-specific-page/


You can use this function:

<?php if(is_page('your-page-slug')): ?>
    //do nothing on selected pages
<?php else: ?>
    <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" />
    <script>
        (adsbygoogle = window.adsbygoogle || []).push({
            google_ad_client: "ca-pub-********",
            enable_page_level_ads: true
        });
    </script>
<?php endif; ?>

If you want to disable this on more than one page, just add them like this:

<?php if(is_page('your-page-slug1') || is_page('your-page-slug2')): ?>

Wordpress Developer Resources are very useful (more than codex), so i recommend checking them out first if you don't know how to do something

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.