Embed Google Adsense Ad Anywhere Inside The WordPress Post Without Using Plugin

If the post that you are writing is a longer one, you might want to insert or embed an adsense ad in between the content so that you can monetize your longer content.

You can achieve this by pasting a small function in your active theme’s functions.php and then using a simpe shortcode [adsense]

Code to paste in functions.php :

function create_adsense_shortcode( $atts ) {
extract(shortcode_atts(array(
'size' => '1'
), $atts));

switch ($size) {
case '1' :
$ad = '<div class="inpost_ad">
<script type="text/javascript"><!--
google_ad_client = "pub-xxxxxxxxxxxxxxxx";
/* 336x280, created 4/8/09 */
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>';
break;
case '2' :
$ad = '<div class="inpost_ad">
<script type="text/javascript"><!--
google_ad_client = "pub-xxxxxxxxxxxxxxxx";
/* 468X60, created 4/8/09 */
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>';
break;
}
return $ad;
}
add_shortcode('adsense', 'create_adsense_shortcode');

Replace the google ad (the code between the div start and div end) with your own ad code (remember to check your pub-ID i.e. google_ad_client, I have replaced it with xxxxxxxxxxxxxxxx and also check the google_ad_slot for the correct ad).

How to use the shortcode

In the post content, you can directly write/put the shortcode [adsense] anywhere to embed your ad.

How to embed different size of ads

You can embed different sizes/format of Google ads by making a slight addition in the code. In the above code, you can refer to second case statement:-

case '2' :
$ad = '<div class="inpost_ad">
<script type="text/javascript"><!--
google_ad_client = "pub-xxxxxxxxxxxxxxxx";
/* 468X60, created 4/8/09 */
google_ad_slot = "xxxxxxxxxx";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>';
break;

So, replace the Google adsense code with your second adsense code.

In the WordPress post content when you need to insert this ad, just use this shortcode [adsense size='2']. You can also create third ad with different size, just add a new case statement (similar code as above, just replace the ad code with third ad).

How to format or style these ads

Now you would like to make these ads float left or right, or give some background color or margin and padding. It is very easy, you can style the div with class ‘inpost_ad’, i.e in your active theme’s style.css, you can add similar kind of code:

.inpost_ad{
float:left;
margin:5px 0 5px 10px;
}

You can add any kind of styles to this class, that would help you styling the ad container.

Post your comments to tell us how it worked for you.

4 Comments

  1. Nicholas Teo@Feng Shui since 1988

    December 10, 2010 at 6:41 pm

    Very detail but I still find it a lot easier to use a plugin. I hate to mess around with the code.

  2. Sarkari Naukri

    December 10, 2010 at 9:20 pm

    Hey it is a great tips. I have trial your advise and success. But Can you help me what should i do if i want to make ads show at the beginning of the post not the end of the post.

  3. raybak

    December 15, 2010 at 6:21 pm

    that is so great I dont want to use plugins for displaying adsense in post this will work for me.

  4. Asif

    June 20, 2012 at 11:41 pm

    Well i don’t like to mess around with codes. Is there any plugin?

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>