Adsense in Content of Posts and Pages for wp3

A flexible way to add google adsense into posts and pages in your wordpress blog.
This appraoch uses shortcodes which are an underused technique of increasing the functionality of themes.
Through the formatting options in the shortcode, the text can flow around the adsense blocks similar as it would happen with inserted images.
Options for the alignment are ‘left’, ‘center’, ‘right’ – anything else will be treated as ‘none’.

The code that needs to be added to functions.php of the theme is shown below:
it is called in the text with: [ a d s e n s e     f l o a t = ' l e f t ' ]

// allowed parameter: 'left', 'center', 'right', 'none';
// other entries will be treated as 'none';
function add_adsense($atts) {
  extract(shortcode_atts(array(
  'float' => 'none',
  ), $atts));
if( $float == 'left' ) $float = ' style=" float:left; margin: 10px 10px 5px 0; "';
elseif( $float == 'center' ) $float = ' style=" display:block; margin: 10px auto; text-align:center; "';
elseif( $float == 'right' ) $float = ' style=" float:right; margin: 10px 0 5px 10px; "';
else $float = '';
$ads = '<span class="adsense "'.$float.'>
<script type="text/javascript"><!--
google_ad_client = "pub-0123412341234123";
/* adsense_in_posts */
google_ad_slot = "1234512345";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</span>';
return $ads;
}

add_shortcode('adsense', 'add_adsense');

You can see the result here entered into a passage of ‘lorem ipsum’:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse in libero libero. Morbi porttitor, sapien et aliquam malesuada, augue elit tincidunt tortor, vel convallis orci felis quis turpis. Maecenas sit amet diam ut nibh rhoncus accumsan in eget urna. Vivamus sed tempor odio. Curabitur ultricies ultricies molestie. Maecenas eu nisl elit, vitae auctor eros. Nam quis risus libero. Nunc nec ligula sem. Nullam eget sem sed nisi porttitor imperdiet. Etiam ac sapien sed est vestibulum iaculis et id mauris. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Integer tortor magna, malesuada id imperdiet eu, fringilla eget sapien. Donec augue sem, semper et ultrices a, cursus eget nisl. Duis dui enim, ornare ut facilisis eget, egestas ac orci. Etiam viverra varius erat, id feugiat tellus egestas eget.

Aliquam sem purus, ornare ac tempor nec, accumsan ut felis. In cursus massa vitae mauris iaculis nec semper dui commodo. Proin fermentum, lacus ut accumsan scelerisque, orci dui sodales arcu, ac mollis neque odio id turpis. Mauris sagittis tincidunt ultricies. Sed diam ipsum, posuere quis scelerisque et, malesuada quis orci. Mauris nec lectus ac nibh interdum aliquet.

PS: I found more examples of shortcodes in tuttoaster.com

10 Replies to “Adsense in Content of Posts and Pages for wp3”

      1. I finally got the coralmotters blog to link to my Adsense account. Part of the problem was that my publisher ID was not correct and all attempts to edit it did not work.Maybe this will help others who are trying to link their blog to an existing Adsense account. First I deleted the old Adsense page element (the one that did not link to the publisher ID of my Adsense account). Next, I added a new Adsense element. When prompted I added my account information. This time it grabbed my correct publisher ID. I think the previous error in my Adsense identity arose from the beta blogger days. However, the EDIT link (if you’re trying to change the adsense ID) did not work. Good luck!

    1. AdSense does pay the highest but you should still test other methods of earning money to see which one brings in the most money.

      Try testing with CPA offers, clickbank products, amazon affiliate, ebay affiliate…

      You should always be testing different monetization strategies.

Leave a Reply to alchymyth Cancel reply

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