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 :
[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”;
/* 336×280, 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’);[/php]
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.
[recommended]You would like to read:- WordPress plugins to Rotate Advertisements including Google Adsense, Chitika, YPN or Banner Ads[/recommended]
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:-
[php autolinks=”false”]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;[/php]
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:
[code].inpost_ad{
float:left;
margin:5px 0 5px 10px;
}[/code]
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.