Responsive Adsense Plugin for WordPress

Responsive Adsense Plugin

I recently posted about a way to display different sizes of adverts depending on the window size. I’m sure it could be useful for a lot of people, so… I made it a WordPress plugin.

It’s a simple widget that works with your Google Adsense “Publisher ID” and some of your Advert Units IDs.

It will display on your pages the bigger unit that can fit in the widget container.

Enjoy folks !

Make your Google Adsense blocs fit your responsive design !

This is actually quite simple. The thing is to set the ad parameters depending on the window’s size before we call the show_ads.js script.

google_ad_client = "ca-pub-XXXXXXXXXXXXXXXX";
if ( window.innerWidth>979 || window.innerWidth<768 ) {
  // If the window is larger than 979px or smaller than 768px, we set the first bloc
  google_ad_slot = "XXXXXXXXXX";
  google_ad_width = 300;
  google_ad_height = 250;
} else {
  // Otherwise, we set the second one.
  google_ad_slot = "XXXXXXXXXX";
  google_ad_width = 200;
  google_ad_height = 200;