WordPress is an excellence tool for blog content management. I just started to learn how to customize the theme and put it online (theme for this blog). I was having a problem of inserting Google Adsense Code into my post article. I want to position the Adsense differently for my different post, at anywhere as I wish, not just at a fixed position as coded in the template files.

You may think that why don’t just do a simple copy and paste? By doing that I may end up displaying many Adsense at my home page. The home page is displaying a list of extract from many of the posts. Google only allow a maximum of 3 Adsense to be displayed in a page.

I searched the net and unable to find a plug-in that can automate this task for me. Eventually, I found the web-templates.nu’s get_the_content() WITH formatting and Ofzenand Computing’s How to Code Social Networking/Sharing Buttons for Your WordPress Blog, that inspired me to modify my theme to solve my problem. I will walk through the pieces of code to show you how this modification works.

A WP plugin version of this code is now available.
Get it now at Quick Adsense

Template Files Modification

The process isn’t too complicated. You just need to add some PHP & HTML code into your WordPress theme. You must have the basic understanding on how to edit the PHP files (your theme template) before we can start. If something goes wrong, you may not be able to render your blog correctly. I suggest you backup your theme template before doing any modification.

Modify functions.php

Basically the process is to put HTML tags ( <!--Adsense1--> ) anywhere within the post article, and call a PHP function in your template file to replace these tags with your Adsense Code.

Append the following PHP code to the end of your functions.php theme template file (Appearance > Editor > Theme Functions). Replace the Adsense Code block with your Adsense Code:

function get_the_content_with_formatting ($more_link_text='(more)', $stripteaser=0, $more_file='')
{
   $content = get_the_content($more_link_text, $stripteaser, $more_file);
   $content = apply_filters('the_content', $content);
   $content = str_replace(']]>', ']]&gt;', $content);
   return $content;
}

function AddGoogleAds ($content)
{
$Adsense1 = <<<ADSCODE1
<div style="float:left">
<script type="text/javascript"><!--
google_ad_client = "pub-9999999999999999";
google_ad_slot = "0000000000";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
ADSCODE1;

$Adsense2 = <<<ADSCODE2
<div style="float:right">
<script type="text/javascript"><!--
google_ad_client = "pub-9999999999999999";
google_ad_slot = "1111111111";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
ADSCODE2;

$content = str_replace(array('<p><!--Adsense1--></p>','<!--Adsense1-->'),$Adsense1,$content);
$content = str_replace(array('<p><!--Adsense2--></p>','<!--Adsense2-->'),$Adsense2,$content);
return $content;
}
 

Modify single.php

Look for the line <?php the_content('(more)'); ?> (parameter may be difference) in your single.php theme template file, and replace it with the following code:

<?php
   $cont = get_the_content_with_formatting();
   $cont = AddGoogleAds($cont);
   echo $cont ;
?>
<?php /* the_content('(more)'); */ ?>

Insert Adsense HTML Tags to Your Post

That’s all you need to do with your theme template. Now, switch to the HTML tab in your WordPress editing panel and insert <!--Adsense1--> or <!--Adsense2--> into your post article, anywhere as you wish (modify the code in functions.php for more Adsense options). View your article page and see your Adsense appear at the desired location.

This modification should work for WordPress V1.0 & higher, but it has been tested only in my V2.7.1. Have fun! You are welcome to submit any feedback.

Related Articles
External Links
Comments

4 Responses

  1. Thanks for this tutorial. It’s very useful, i’ve tested it and work perfectly :)

Leave a Reply

Feed subscribe via feed, or

get the email updates :

It comes with the intention to highlight anything about blogging & technology that appear to be interesting, beside the useful applications & tools. This is a place for bloggers & tech lovers, and for everyone who know how to use a computer.
I wish to announce that I'm no longer be the owner of the Quick Adsense Wordpress Plugin. The plugin previous homepage has been redirected to a new page ... (read more)
Popular Posts
Power Your PC With 45 Free Applications Adsense Format And Position Optimization Multiple URLs Opener Stumbleupon & Digg Thumbnail Maker Install WordPress In Your Local Computer With XAMPP 34 Excellence Online Photo Editing Websites Clear Your StumbleUpon New Shares From Friends Even Faster Draw Yourself Into A Cartoon Avatar

Recent Comments

  • CTR Rings : This is very useful tool to use. I always use this. (more)
  • Ramy_Yous : Thanks for the Websites :) , Really Good and Helpfull ; Thank you ! :) . (more)
  • International Web Development : It is very nice tool to open multiple urls in click, i am... (more)
  • CTR Rings : hey thanks, yes I will also definitely try this plug in as it seems to be very... (more)
  • Jon : Oh no! The plugin is broken, does not work with the latest version of Wordpress.... (more)