Site logo

Archived topic

Hide AdSense ads on Mobile device

11 replies · Started by Pintu on August 22, 2022

Viewing posts 1–12 of 12

Hi, I've added 970 x 90 AdSense Ads in GeneratePress after header using Hook > after_header. Though the ad shows fine on desktop, and I've no problem with it.
But I don't want to show the ad on a Mobile device, because it is a fixed Ad and doesn't show that great on mobile. Therefore, how can I hide this particular ad on a mobile device?
I've added my site link in the private section, please help. Thanks in advance.

Hi Pintu,

Are you using a Hook Element?

Yes

You could try enclosing the ad code in a php conditional statement. Example:

<?php 
if( ! wp_is_mobile() ) { ?>

<!-- add ad code here -->

<?php } ?>

How can I do that with the hide-on-mobile class?

<div align="center">
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-74341"
     crossorigin="anonymous"></script>
<!-- After Header Ads -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:90px"
     data-ad-client="ca-pub-74341"
     data-ad-slot="4230300"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>

This is the ad code I've added, and I wanted to know if can I use the GP hide-on-mobile and hide-on-tablet classes to do the job.

Is David here?

David’s out of shift right now.

Using hide-on-* for ads is not allowed by Google unless it’s a responsive ad unit.

Are you using a responsive ad unit? If so, here’s how to do it: https://support.google.com/adsense/answer/9183363?visit_id=637968240625154645-2866526188&rd=1#hide&zippy=%2Chiding-ads-for-specific-screen-sizes-example:~:text=screen%20width%20example-,Hiding%20an%20ad%20unit,-In%20certain%20cases

Otherwise, only adding the script through a PHP conditional statement should work as well.

Okay, so the new code should be like the following?

<?php 
if( ! wp_is_mobile() ) { ?>

<div align="center">
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-74341"
     crossorigin="anonymous"></script>
<!-- After Header Ads -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:90px"
     data-ad-client="ca-pub-74341"
     data-ad-slot="4230300"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>

<?php } ?>

Yes. Make sure to enable Execute PHP.

Thanks, Fernando for your help. It worked :)

You’re welcome Pintu! :)

This archived topic is closed to new replies.