Previously we have talked about how to hide AdSense unit in responsive design with CSS on certain devices. Also, we talked about PHP snippet to serve different AdSense units on different devices. Here is Easy PHP Snippet to Hide AdSense Unit on WordPress 404 Page to Avoid Policy Violation and Decrease False Impression, False Low CTR. Technically, this is last problematic point which among the commonly faced problems.
Situations Which Can Force Us to Think to Hide AdSense Unit on WordPress 404 Page
Normally if we use free WordPress plugins like Ad Injection, question of specially coding to hide them on 404 pages will not appear.
But in real we sometimes need to use someway to inject at least the top unit fully manually. Usually that unit is below navigation. This creates a funny situation on the 404 pages. Unknowingly we violate an AdSense policy. Showing Ads on pages with autogenerated content or 404 pages violates AdSense policy. Technically, you should hide on pages like HTML sitemap, even contact us page. If you use the same type of WordPress conditional logic, you can hide on such pages too.
PHP Snippet to Hide AdSense Unit on WordPress 404 Page
We need to wrap the Google AdSense Ad units with PHP and WordPress Conditional Tags :
---
1 | https://codex.wordpress.org/Conditional_Tags |
We can omit 404 pages with ! is_404()
:
1 2 3 | <?php if ( ! is_404() ) : ?> // do anything here <?php endif; ?> |
With real like AdSense code, it will become like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php if ( ! is_404() ) : ?> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- your_unit_name --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-xxxxxxxxx" data-ad-slot="xxxxxxxxx" data-ad-format="auto"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <?php endif; ?> |
Now, take that, you want to hide an AdSense unit on 1080 CSS px wide devices as well as on 404 pages. Then you can use, it is CSS plus PHP :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php if ( ! is_404() ) : ?> <div class="wrap google_ads"> <style type="text/css"> .adslot_example { display:inline-block; width: 728px; height: 90px; } @media (max-width:1023px) { .adslot_example { display: none; } } </style> <!-- your_unit_name --> <ins class="adsbygoogle adslot_example" data-ad-client="ca-pub-xxxxxxxx" data-ad-slot="xxxxxxxx" data-ad-format="horizontal"></ins> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script>(adsbygoogle = window.adsbygoogle || []).push({});</script> </div> <?php endif; ?> |
We can use wp_is_mobile()
for detecting mobile devices to exclude :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php if ( ! wp_is_mobile() ) : ?> <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- your_unit_name --> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-xxxxxxxxx" data-ad-slot="xxxxxxxxx" data-ad-format="auto"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> <?php endif; ?> |
We can combine multiple WordPress Conditional Tags. We used the ||
to check for any of the conditions like OR. We can use the &&
to create an AND condition, in which both the condition must be met in order to display the item. The !
is used for excluding from logic.