Insert Google Adsense Code Anywhere In Your WordPress Article

25 Comments April 1, 2009  |  by Philip Ze
Categories : Technology Internet
Share

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.

A plugin version of this code is now available.
Download from TechMilieu.com

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.

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.

Comments

Say somthing about this post

Trackback

Those who link to this post

  1. SteallRap says:

    Great site this http://www.flexijourney.com and I am really pleased to see you have what I am actually looking for here and this this post is exactly what I am interested in. I shall be pleased to become a regular visitor :)

  2. thomas beatus says:

    Thank you! This is exactlly what i was looking 4. clear to the point.

  3. Jessicages says:

    I really liked this post. Can I copy it to my site? Thank you in advance.

  4. Pineapple1 says:

    Hi there, I copied your code and replaced my publisher number, and entered the HTML tags into posts, but it’s not working. Any help would be greatly appreciated!

  5. Pineapple1 says:

    Looks like it works with single posts, but not in the full page.

    • Philip Ze says:

      @Pineapple1: You need to modified both functions.php & single.php as posted above, and it should be ok. Take note that you should replace both your publisher number & ad_slot. As for single.php, inside single.php should be replaced with the code mentioned above.

      Yes it work on single posts, I’m not sure what do you mean by full page, is it your home page (index.php) ? If so, you will need to insert the whole google adsense code directly into index.php.

  6. Sandeep says:

    Great site and I am really pleased to see you have what I am actually looking for here and this this post is exactly what I am interested in.

  7. maimax says:

    OHH Some very interesting and insightful thoughts. I like this.

  8. John Pleiter says:

    Hi,

    Great post! This is exactly what I have been looking for. Thanks for sharing.

    I’m just starting to make some money with my first adsense site at BodyArtEzine.com and realize that my CTR could be much higher with better ad placement.

    At the moment my google ads stand out like proverbial dogs balls, excuse the pun. I’ve heard of instances where CTR’s have been increased 10 fold through optimization with better ad placement, so I’m very keen to implement this.

    Problem is, I’m a complete newb to dealing with php so I’d be very grateful if you could explain exactly where to append the code to the functions.php file. From my understanding the code should be enclosed with php script tags and I don’t see those with your code.

    I’ve tried several different possibilities but can’t get it to work.

    Hope you can help,

    Regards,
    John.

    • Philip Ze says:

      @John Pleiter: The functions.php is a file uses directly by WordPress for php functions, so the php script tag is not required in this file.

      To make it work, use notepad to open the functions.php file, copy the php code mentioned above, replace the Adsense Code block with your own Adsense Code, and simply append the whole code to the end of your functions.php

      The next step is to modified the single.php. Follow the instruction mentioned in the post above. Do remember to backup your theme template before doing any modification.

  9. John Pleiter says:

    Hi Philip,

    Thanks for the advice but it doesn’t seem to work with the theme I am using, which is a Grunge Mag. I did exactly like you said and all the code is visible above my header with the adsense unit on top of it and I also lose my articles. It must need some modification or extra code for it to work with my theme.

    I have a couple of different themes on other blogs I might try it on them and see how it goes.

  10. Sommy says:

    Please I am totally a new to blogging and I do not understand html. I have my adsense code but when I copy and paste it nothing appears, it just disappears and nothing comes up. Is there anything I can do to because the adsense guide videpo from adsense team has not helped me. I am frustrated really.

  11. Sommy says:

    I forgot to add that I am using a word press blog but hosted by a blogging website, was wondering if this is why I can’t add adsense to the blog.

  12. Great idea, but will this work over the long run?

    • Philip Ze says:

      Hi Lucie-Mueller,

      Yes, it should. As long as the WordPress function like get_the_content() is not depreciate in future version of WordPress, and I believe it will not.

  13. Gossips says:

    How about adsense will appear random on the single post. Is it possible?

    Thanks

  14. Gossips says:

    Wow.. That’s cool.. Thanks for fast response. It is highly appreciated…

  15. I appreciate you for helping me with how to insert AdSense into blog posts my friend. If you come out with any new plug ins, please comment my blog and let me know directly. Thanks again and hope you keep in touch =)

    http://www.ShawnDrewry.com

  16. Carol says:

    Thanks a lot for yet another first-rate WordPress tutorial. I am always on the look-out for awesome WordPress tips to suggest to my readers. Thanks for taking the time to write this post. It’s exactly what I was searching for. Truly great post.

Leave a Reply

Get a Gravatar if you like to show your picture with your comments.