Mobile traffic is a growing trend that will soon surpass desktop traffic – this is an undeniable fact.

For many websites, mobile is already the #1 source of traffic. So how do you make sure that you mobile site visitors see better performing ads?

What I did is replaced a Responsive ad unit on my sites (which I bought in Jun 2016) with 2 fixed size ads (one for mobile and one for desktop) and achieved 104% increase in AdSense Income the first month (here is proof)!

There are MANY ways to go around this – I recommend that you check out my Mobile Ads optimizing guide.

In this post I will teach you how to display Mobile optimized ads for smart phone users on the fly, and provide simple PHP code and Instruction on how to implement and use it, to DETECT mobile visitors to your website and automatically display Google AdSense ad which is optimized for mobile.

By OPTIMIZED I refer to better looking ads on mobile, which provide better CTR, CPC, and ultimately better earnings!

Before you begin, see How I DOUBLED my website AdSense income, by Mobile-optimizing just one mobile ad – VIDEO:


Here is what “old” and “new” ad looked like (old ad was a Responsive ad unit) replaced by a rectangular ad:

Optimizing Mobile Ads for better Click Through Rates, Cost Per Click and RPM

As you can see from above video, it is essential to optimize the mobile version of your website, to increase your ad income. Now let’s get to auto mobile ad switching.

If you don’t want to go into code:

To implement all the things I show below, you will need to go into your WP theme code and make few changes. If you make mistake, you can EASILY break your site. So if you don’t feel comfortable doing this you can get my WP AdSense theme, where I’ve done all the work for you.

The above theme does what I will teach you in this guide – automatically shows appropriate ad size for MOBILE, DESKTOP and TABLET users!

PHP code to Detect Mobile User-Agent and automatically show optimized ads

What this code does is pretty ingenious – it detects if your site visitor comes from a mobile browser by reading USER-AGENT, and based on that, shows an ad size specific to mobile device resolution.

For example – a 728×90 Leaderboard ad would look horrible on iPhone screen, and without knowing if user comes from desktop or mobile your site would display a default ad size (unless you use Responsive ads which are notoriously bad for your RPM and Income) OR you would have to use a separate mobile theme OR a dedicated mobile site version (like

So this PHP user-agent script solves so many problems at once, and it’s not a plugin, so you know exactly what code you are using.


Messing with PHP code of your theme, can easily break your website, so do this VERY carefully, and ALWAYS backup theme files before editing them!!!

I’m not responsible if you mess up your site!

Having BASIC understanding of PHP and HTML syntax is very much recommended

Now that you understand potential disastrous consequences, if you mess up your PHP code (usually it’s simple syntax mistakes, like forgetting a closing } bracket or a semicolon) we can proceed with the actual code and how to use it.

Below is the code itself – I know its not pretty, but gets the job done 100%.


$useragent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $useragent) || preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i', substr($useragent, 0, 4))) {
$_SESSION['isMobil'] = true;
$_SESSION['isMobil'] = false;

Setting up Mobile User detection script

So now we need to add this script to our site. I include this in code the HEADER.PHP file of my WordPress theme.

To keep the code cleaner, I have actually created a ismobile.php file in the root folder of my theme, and include it right after opening HTML tag (don’t forget PHP open and close brackets):

<?php include 'ismobile.php'; ?>

So now, each time a site visitor comes, the script above saves into SESSION if the visitor is a MOBILE or DESKTOP user. From there we can easily show a specific ad optimized for each platform.

NOTE: Most WordPress or other CMS themes will not have “ismobile.php” so you will need to upload via FTP a blank PHP file tot he root folder of your theme or create it in the cPanel File Manager.

Including MOBILE and DESKTOP ads on your site:

So there are many ways to do this, and this will depend on the Theme that you are using. You will most likely need an FTP client and access to your site, unless you want to add extra code in your theme templates (such as Header, Single, Page, Content, etc).

I cannot possibly describe ALL options, because each theme is so different, so you will need to watch the video below and kinda figure this out on your own.

Use PHP simple IF/ELSE statement to display either MOBILE or DESKTOP ad unit

<?php if ($_SESSION['isMobil']) { ?>

       <!-- MOBILE Ad Code Goes here -->

<?php } else { ?>
       <!-- DESKTOP Ad Code Goes here -->

<?php } ?>

Copy and paste the code above into you Theme file … for example in SINGLE.php or CONTENT.php (depending on your theme).

I usually place a TOP ad unit between page/post title and content. In WordPress content is included with this function:

<?php the_content(); ?>

So what you can do to include your ads below page title on EVERY page or post, is:

  • 1) Create file ad-top.php and include it directly above content function, or
  • 2) Paste your IF/ELSE code with AdSense snippets directly into theme template, above content function.

I always create a separate file for each ad placements. This makes it easier to quickly change or swap or A/B test ads in any spot on my site, by changing one file in Theme Editor – no need to go to every template.

So here is what my code typically looks like when I include a TOP ad between page title and content:

include 'ad-top.php';

Above example assumes that your template and ad-top.php are in the same folder of your theme. If your page templates are located in weird folders (for many themes they are), what I do, is create ads folder in the root directory of my theme, and then use PHP function to call files from the root directory. You can use it all the time, just to avoid issues.

include( get_template_directory() . '/ads/ad-top.php');

Keep in mind that include( get_template_directory() . ‘/ads/ad-top.php’); works only in WordPress. So if you are using Joomla, Drupal, other CMS or custom-built PHP site, this will not work.

Basically if you read this, you will most likely figure it out.

Including mobile optimized ads inside the content:

Examples above covered only automatic inclusion of ads in your Theme / template. But what do you do if you want to include ads in content … say after paragraph 4?

You can use various AdSense plugins, but I strongly advise against this. What I do recommend (and what I do) is this:

  • I select 10-15 post and pages on my site (based on visitors) and manually include 2-4 ads in content (note that recent AdSense policy update allows to have more than 3 ads per page).
  • I use WP Include plugin to include a PHP file with IF/ELSE statement and Ad Codes for mobile and desktop users.
  • You can even do A/D testing to find out which ad format make you the most money!

So it’s easy to say, but here is the problem:

WordPress does not allow you to execute PHP code inside Post/Page for security reasons! There is a plugin to allow PHP code in posts, but I won’t name it, because it’s easily hacked, and I actually lost over $1000 because of this (my best ever site got hacked multiple times in Aug/Sep of 2014 and I believe it was because of that plugin).

Each day that my site was hacked, I lost about $200-250!!!

So now I’m using another plugin – it’s not in WordPress plugins directory, is not supported by its creator anymore, and the website for this plugin is down… I will make this plugin available soon.

Basically what I do to include AdSense ads in content, is the following – I use this short-code:

[ad file="ad.php"]

Modify this for the particular ad file i want to include and past this in different places inside my articles/posts.

Now inside those includes PHP files I can run any code I need, and I do experiment with this quiet a lot. I often run an AB Test in different ad placements.

For example, for my top ad unit I can test 2 different ad sizes – 728×90 or 336×280. I can further test alignment of the rectangular ad unit as well as wrap content around it, to see which combination earns most money!

For AB Testing I use PHP shuffle function which displays each ad equal number of times. If I test Leaderboard ad vs Large Rectangle ad (2 versions) and page gets 1000 views, then each version get about 500 views (+/- 1-2%).

However AB Testing is a HUGE topic in itself, so i will make a separate post about it.

I hope this helped you, and increased you ad earnings!

BTW, all of the features described here are included with my AdSense WP Theme and it’s really easy to use – you won’t need to do any coding and can start implementing and AB testing your ads on both MOBILE and DESKTOP platforms right away.

Also, all of this I do teach in my AdSense Success course on Udemy! And if you take the course, you get my WP Theme for free!


  1. it is possible to implement this on blogspot?

    • John – most likely no – I have zero experience with Blogspot, but I’m 99.99% sure you can’t run your own PHP code on Blogspot, because that would be a massive security risk, so no… but I do think there ay be some sort of plugins for this …

      In any case – you should move to self-hosted WP or something … especially if you use your own domain name.

      I have a site that was on blogger … I moved it to WP (self hosted). I bought it for $3300 and it has earned almost $7k since in first 12 months. Most of it is due to switch from crappy Blogger theme to full control of WP … But that site had it’s own domain name… like www .my3300dollarsite .com … not something.blogger. com

      Best of luck

  2. What better unit ads shows in articles or in feed for mobile seen.

    • On mobile, best to use 250×250 or 300×250. Just don’t use 300×250 above the fold – it’s against adsense TOS.

  3. Please Tell me that how we place best place for ad code in our articles with size..

  4. Is there a plugin that I can install which automatically does all the above? I will be a great help. I am trying to setup a viral website on one domain and ecommerce website on other domain. Please help , would appreciate it.

    • Yes – our AdSense Ninja plugin. It’s not free however … so if you want free, you can use the PHP code – it will work for WP or any PHP based site, if implemented correctly.

  5. its a really helpful but what for those countries where you don’t get high CPC ads…our earning not that much from AdSense to pay $179 for this plugin…

    • Shekhar,

      What is your traffic / income per month? Also what country does most of the traffic come from?

      CPC is not everything. The effect of our plugin is usually higher CTR (more clicks) as it positions ads where they are more likely to get clicks.

      Ads Ninja WP plugin - increase in CTR, RPM and earnings

      As you can see in screenshot above, while CPC went down by 20% the RPM and CTR about doubled! All this is from when i began to use my own plugin 🙂

      I also have several websites in Asia where CPC is very low compared to US… Like $0.01 – $0.05 per click… Still i manage to increase the RPM of the website by 40-80%.

      Besides i don’t see many ads on your website anyway. So just displaying ads will increase your earnings!

      Our plugin is configured to show ads on all existing and new posts/pages (you just have to enable it in settings), and you would start monetizing all your content immediately.

      Even if you make $200/mo now and bump earnings or rather revenue per 1000 page views (RPM) by 30% … The plugin will pay for itself in 3 months! For me 30%is pretty conservative number, because with relatively small amount of optimizing, you should easily do that or even much better.

  6. Thank you for this post it was an eye-opener. Please, how do you add Ad codes within posts? You mentioned that you’ll release the plugin you use. Please help.


Leave a Reply

Your email address will not be published. Required fields are marked *