This code working partially, need your support

A few days back I have asked help to “Remove Adsense serving to a particular page” on a WordPress Blog.
The page is Blog SubPage no. 2 [domain .com /blog/page/2]

I have tried this code and removed Adsense Script only, but looking for the code to remove the entire Adsense Code on that page (Blog subpage /blog/page/2)…

function conditional_adsense() {
if (!is_paged()) echo '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>';}
add_action('wp_head', 'conditional_adsense');

I have removed the Adsense JS Script from the blog sub-pages but am seeing a Yellow Space instead of code. Kindly help me to construct code to remove the particular code as well.

1 Like

Hey there,

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')): ?>

Is it the function you are asking ?

You need to remove on all sub pages or only specific sub page ?

You dont need to add this code to functions.php just as casual add this to header.php where you add adsense script but we will use php IF Functionality.

I hope this helps you.

I tried this code, but when adding this code to functions.php returned an error. That’s why I am looking for an alternate solution and found one that gave a partial solution.

Please read my message clearly, as I have mentioned that problem is with the subpage no. 2 only (domain .com/blog/page/2)…

<?php if(is_page('/blog/page/2')): ?>
//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; ?>

You can add this to the ad widget where you need .

I guess you can use the regex pattern for these types of pages.

Checkout that guide if it can help you.

1 Like

Hi @supx Can you create a code for me?

Sorry I am not interested to use Plugins.

Actually I don’t know PHP.

But I think at this part, you could use some sort of regex.

if(is_page('/blog/page/2'))

What’s your requiement again? You want to hide the adsense box as well?

Have you tried this method?

What it does? It loads AdSense script for all pages except at /page/2/ permalink.

I have a simple solution . Let me know is it on sidebar ? you applied via a widget ?

If possible send me the page via IB so i can tell you a simple solution :slight_smile:

The code I created does the same function brother. But I need to remove the Adsense Code as well as Adsense Script.

Now the Adsense script was removed but not the Ad Code.

Brother, Yes, The AD was on Sidebar and one in Footer. But both the ads are sitewide and generating considerable revenue. :pensive::pensive:

There is no benefit of doing so, as advt is already not going to load without adsbygoogle.js script on /page/2/

We can use css to hide the total div . every page has unique id :slight_smile:

Just send me the sub page link. I will give you a simple solution :slight_smile: