How to Integrate Google Adsense into WordPress Shortcodes

WordPress is such a great tool for bloggers and publishers. I`m not making any differentiation between digital writers and others because WordPress, being a CMS, can handle any type of website. But as much as they love to offer free content to the masses, they still have to monetize WordPress in someway and what`s the most convenient way if not Google Adsense?

Integrating Google Adsense into your WordPress theme and displaying it on posts can be done by editing the single.php file. Although this solution works just fine, you`re still limited to the ads you display. But what if you want to add Google ads anywhere in a post? And not only that, what if you want to have some ads in a certain post and other ads in other posts? The solution is to make use of WordPress shortcodes.

I want to show you how you can integrate Google Adsense Ads into any post using shortcodes. For this, we have to create a shortcode for Google Adsense.

All custom functions we built for WordPress have to be added inside functions.php file. If you don`t have one, create one and paste the next code in it(make sure to use the beginning and ending php tags):

function adsense_ads( $atts, $content = null ) {

	extract( shortcode_atts( array(), $atts ) );

	$ad='<script type="text/javascript"><!--
		google_ad_client = "ca-pub-0000000000000000";
		google_ad_slot = "0000000000";
		google_ad_width = 336;
		google_ad_height = 280;
		//-->
		</script>
		<script type="text/javascript"
		src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
		</script>';

	return $ad;
}

add_shortcode('adsense', 'adsense_ads');
Make sure to replace the current google_ad_client and google_ad_slot ids with your own ones. Each google_ad_slot is unique for every ad created.

Now, you can use the shortcode [adsense] in any post or page you want and the ad specific to the google_ad_slot you provided will be rendered. But this is not a very flexible shortcode, right? Let`s make a step forward and extend this shortcode. We`re going to make it render not only an ad, but any type of Google Adsense ad. We`re going to extend “google_ad_slot”, “google_ad_width” and “google_ad_height”. Also, we can wrap the ad block into a div so we can stylize it or center it on the page.

function adsense_ads( $atts, $content = null )
{
	extract( shortcode_atts( array(
      'slot' => '0000000000',
      'width' => '250',
      'height' => '250',
      ), $atts ) );

	$ad = '';

	$ad.='<script type="text/javascript"><!--
		google_ad_client = "ca-pub-0000000000000000";
		google_ad_slot = "'. $slot .'";
		google_ad_width = '. $width .';
		google_ad_height = '. $height .';
		//-->
		</script>
		<script type="text/javascript"
		src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
		</script>';

	return '<div class="google_ad_block">' . $ad . '</div>';
}

add_shortcode('adsense', 'adsense_ads');

Notice the 3 parameters we give: slot, width and height. If you insert a shortcode as [adsense] into a post, an ad of 250x250px and with the slot id=”0000000000″ will be rendered.

To display a new shortcode with other dimensions and google_ad_slot, use the extended shortcode call:

[adsense slot="1234567890" width="336" height="280"]

This is how you can easily integrate Google Adsense ad blocks inside any place of your WordPress theme, without the need of editing theme files every time you want to change something. Shortcodes are truly amazing and without a doubt you can make the most out of WordPress using them!

[Total: 1    Average: 5/5]

Madalin Tudose

I`m a web developer. I build HTML Templates and WordPress Themes which are eventually released on ThemeForest under the DeliciousThemes brand.

Join the discussion

  1. Chris on December 20, 2012 at 2:46 pm Reply

    Good stuff. Thanks for posting this article on Google AdWords integration with WordPress. I will keep it in mind next time I need to do this for one of my customers.

    1. Madalin Tudose on December 20, 2012 at 2:57 pm Reply

      I`m glad that you like it! Stay tuned for more tuts like this! 🙂

  2. Mas on January 11, 2013 at 7:53 am Reply

    thx for your explanation

    but didn’t work for me!

    i put every thing in the right way , when i publish the post , i see [adsense]

    and no ads!

    any idea please ?

    thanks again

  3. WpGra on February 26, 2013 at 1:16 am Reply

    Hey Your Brain Has Been Over floated.Thanks A lot. You are a genius man ! Bye bye to mobilepress.Gonna Get it a Try

  4. rash on April 20, 2013 at 6:59 pm Reply

    give a brief description about google adsense (i.e. uses).i dont know what is adsense so i couldn’t understand what you are talking about

Leave a Comment

Your email address will not be published. Marked fields are required.