1

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?

4
  • 1
    Possible duplicate of How to get post ID of the current page/post inside a widget?
    – kero
    Jul 13 '17 at 9:00
  • Even though you are not inside a widget but head, this will work and should answer your question
    – kero
    Jul 13 '17 at 9:00
  • Hi Kero, thank you for your help. My apologies for my ignorance, but how exactly would I apply the answer mentioned above to my case?
    – LucSpan
    Jul 13 '17 at 9:09
  • Make a list of pages' ids you don't want it to display $dont_display = array(1,15,2106);, only display your code, if current id is not one of those if ( ! in_array($post_id, $dont_display)) { /* your html code here */}
    – kero
    Jul 13 '17 at 9:21
2

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

0
1

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/

-2

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/

Not the answer you're looking for? Browse other questions tagged or ask your own question.