0

Remove Google Adsense from showing on mobile devices

As my blog has an responsive design for mobile devices by default, I wanted to know how the Google Adsense ads will be shown. Unfortunately Adsense ads will be the same size as for non-mobile browsers. So I tried to show those ads only if its an non-mobile broser/device.
I wanted to create a function in the functions.php of the theme file which I can use to check if its a mobile browser viewing my site.
For me this has worked:

function isMobileBrowser(){
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
}
add_action( 'wp_head', 'isMobileBrowser' );

This will check for the HTTP_USER_AGENT of the browser. If a normal browser is connecting to your site, the function will return 0 (ZERO not NULL!).
So I surrounded my Google Adsense ads with some PHP:

<?php if(isMobileBrowser()==0 || isMobileBrowser()==false){ ?>
### YOUR ADSENSE CODE GOES HERE ###
<?php } ?>

And all ads were gone on mobile devices!

Leave a Reply

Your email address will not be published.