How to block, disable Adsense ads on specific posts in WordPress

Google Adsense is considered best medium to monetize online content. Lot of webmasters use Google Adsense advertisements on their websites, blogs to earn and make living. Due to strict Adsense requirements, you may want to disable display of ads on specific posts containing sensitive content. You can easily implement this by following method and prevent Google Adsense ads from showing on specific posts.

1. Add following code to header.php file (Preferbally at the top)

1
2
3
<?php Global $ad_flg; $ad_flg=1; ?>
<?php $adsense_blck_post = array(1792,300); ?> // Add post IDs separated by coma e.g 1792 and 300 are post IDs of the post on which you want to block the ads
<?php if (in_array($post->ID,$adsense_blck_post,true)) $ad_flg=0; ?&gt;<br ?="">

2. Now wrap every adsense code with following condition. This change has to be done where you are using actual Adsense code, most likely in single posts and sidebar files.

1
2
3
<?php Global $ad_flg; if($ad_flg!=0) { ?>
//Adsesne Ad code
?php } ?>

You can disable Google Adsense ads on specific posts, by adding post IDs in code as shown in step 1 above. Quick and easy way to prevent Adsense ads from showing up on specific posts on WordPress blog. Credit Sandip Dedhia

SHARE

Comments

  1. Friendship Bracelets says:

    This is cool! i have wristbands site, and a blog. so i just wanted to remove ads on some specific post. n this way it will be very useful. i got here what i wanted to.

  2. Thanks for this great tip. Sometimes, it is very important to disable adsense serving to some posts otherwise Google can block ads to your site.

  3. Hello, I’m interested to know if this can be made to work with other ad networks, or just Adsense? I’m currently working with a CPM network and they requested their ads only appear on family-friendly pages.

  4. Adewale Lagos says:

    thanks for this article, pls can this be used to block add on a page too. Thanks

  5. not working please help getting error on Parse error: syntax error, unexpected ‘?’ in header.php on line 54

Speak Your Mind

*

Subscribe to our mailing list

* indicates required
Close