How To Blend your AdSense Block within the WordPress Post Content?
Ajith | Dec 2008 | About Online Ads, Blogging Tips, How Do I?
A few weeks ago, when I (neatly) blended my Google AdSense 336×280 block a couple of paragraphs down the post content I thought I did a smart job! As a matter of fact, this was really resulting in an increased CTR (Click-Through-Rate) as well and a few readers like Hussein were interested in knowing how I went about doing it? (comment)
However, a few other regular readers like Kim, Lax and Mitch had altogether different views on this topic. Out of their conversation in my last post it was obvious that the regulars who read your blogs on a daily basis (fresh posts that is) should not be presented with the ad block in between though it’s okay for the older posts (seen mostly by search & referral visitors). Now, the following code sample(s) explains how this was achieved.
How to insert ad block for Older Posts alone?
Here, I am talking about inserting ad block in your single post view only and hence you can use this piece of code in single.php file of your theme and nowhere else. Now, if you want to display your ads for only those posts older than say 7 days, I would use the following code (code text file):

You can change the value 7 to anything else like 10 or 30 so that the ad gets displayed on those posts older than 10 days and 30 days respectively.
How to insert the ad block well into the post?
Now if you want to display an ad block after, say, the third paragraph of the post, the following is what you could do:
Withing the single.php, locate the code string the_content. It should look something like the image below:
![]()
Now comment the whole line and include a new little module (yet to be written though we already call it single_post_split.php) below the commented code. The code portion within single.php will now look like:

Add a file by name single_post_split.php to your theme folder and paste following code in that file and save it (Download the actual code from the following text file and rename it with .php extension)

This will now make sure that your ads get displayed after the third paragraph of all single posts that are older than seven days. If you don’t want the older post restriction, just remove the relevant condition in the ‘if’ statement.
You can add more such conditions to insert ad blocks at multiple locations within the post in conjunction with more styles to float them to left, right etc. Another sample code can be found here that displays a 250×250 ad block floated to the right after the first paragraph and a large rectangle ad block after the fifth paragraph.
It is also possible to implement conditions such as inserting ad for all posts except the last two or three etc but more such code could affect the performance of your blog rendering.
A Word of Caution
Back up your single.php theme file before attempting any modification to your theme code.


Hi Ajith – This is a great, and informative post. This is a wonderful way to improve the click through rate.
Barbara Swafford – Blogging Without A Blog’s last blog post… When Your Truth Is Different Than Mine
Simply Good tip this is.
How to alter the adsense code? I saw many of the sites like labnol.org, they will change the code to keep ads in b/w posts..but until you see clearly you can’t detect that ads are there…
That is how they gets $$$
TechZoomIn’s last blog post… TechZoomIn Goals Set for the Year 2009
@Barbara, I appreciate your compliments
@Lax, thanks. Altering AdSense code is against their ToS and such sites will be banned. But Google AdSense also have AdSense API (Beta) that can be used to customize the look and feel of the ads, the way you want it and camouflaged. Unfortunately, this service is available only for AdSense publishers with a minimum of 100,000 page views per day!
Using AdSense API only sites like moneycontrol.com makes a few lakh rupees per month. Have you seen the way they have blended the ads along with their services and other links? Superb it is…
If you are interested in reading about AdSense API, please visit this link http://code.google.com/apis/adsense/
Before anything else, thanks for mentioning my name.
Oh man! You just have had answered my question! I still need to edit some of my post.
Hussein’s last blog post… Welcome Wordpress 2.7
Ajith… BIG THANKS! I just needed that. I recently moved from blogger to wordpress and was looking for something just like this to increase my adsense clicks. Thanks you. I will be linking back to this post tomorrow in my weekly top 5 favorite posts section on sunday.
Kurt Avish’s last blog post… Free Christmas Party Tickets!!
This trick is really cool, and many webmasters are using it.
I recommand you to give Direct code to copy paste, rather than giving those images, images are not clear…
Chetan’s last blog post… Mumbai Terrorist Attack – Now whats the future of TAJ and Oberai ?
One more thing, Calling too many function in your wordpress page is not a good idea, if you are using shared hosting this many cause high CPU usages, so use direct ad code rather than calling pages through functions.
Else use WP-Super cache.
@Hussein, no problem buddy…
@Kurt, thank you very much and I hope you will increase your click-throughs with this trick. And great to know that this little tip is already in your faves.
@Chetan, I have already linked the blocks of code that are more than one or two lines. One-liners should be typed in by you
And thanks for the performance tips. Since I am running this code for sometime now, I know there’s no visible performance problems yet..
Very cool codes; I will be sure to save and look this up again if i go back to using adsense.
Dennis Edell’s last blog post… How To: Evaluating Competition For New Site Niches
@Dennis, thanks… In fact, there are a number of such customizing I have done on various pages but writing about it and documenting is the pain
Great tutorial. i have edited single-post.php for inserting ads but it was between title and post but not like this.
Thanks again. i will implement this and see how is the CTR? whether it improves or not.
Nihar’s last blog post… Take BEFolks First Grand New Year contest and Win Prizes!
@Nihar, thanks… My CTR improved by at least 50-60% after the AdSense was injected into the post paragraphs. However, after some feedback from regular readers I took it out from the latest posts. But it works like a charm!
Wow Ajith, I hadn’t ever thought about popping it into articles by adding something to one of the php codes. I’ve been doing it the harder way, individually, but to tell you the truth I’ll probably stick to that because it allows me to pick and choose which articles to add it to, and which ones to leave alone. For instance, it wouldn’t do me much good to have it on a post with only one paragraph and a video, which I’ll do from time to time. Great going, my man!
@Mitch, thanks man. I understand your requirement. I think for better control for selected articles, it’s better to do it the manual way…
this is an awesome piece of advice!!
i was looking for this, so far it’s working, let’s hope it continues like this!!
thank you!!
This is really cool tip Ajit. But I wonder if this can be done by any plugin. Odcourse its better to self-code rather than use plugin, but its a nice headsup for those coders to code a plugin which does this
Raju’s last blog post… 45 Awesome Websites to Watch Online TV for FREE!
@Det, good to know that the tip was useful for you
@Raju, thanks Raju. In fact, I thought of making this a plugin but realized that for each and every small thing if you start creating plugins that may affect the overall runtime performance of WP blogs.
I think it is better to remove the
<!– –>
..
If the code is there, I usually get an error.. But after deleting it.. Everything works fine.
Hussein’s last blog post… SplendidKid: @PeiProfit So that’s Step 3 is really required?
@Hussein, probably your theme code has a different logic… It was working for me and I actually cut-pasted the code here
Anyways, good to know that you got it working with that change… cheers!
@ Raju
this can be done with the help of adsense manager..but I’m unsure if you can achieve something like putting ads on posts older then 7 days with adsense manager.
@Ajith
Great post..every time I come here…I’m falling in love with your blog.
Thanks Harsh… I have several posts that people haven’t read much
Many of them are not figuring in Google search.
Ajith, if you observe my blog single posts, you will notice that I use intext ads starting from 2nd paragraph – first paragraph is intentionally excluded. I use simple method to do this. Like any intext ad network even ET has section limiting and I use it through a plugin called “Blog Control” – and display the ads after the more break.
Pavan Kumar´s last blog post… Free Website Monitoring Tools to Monitor Website Uptime
@ Raju,
Check out Who sees ads – that has a wide set of options (not tried
).
Pavan Kumar´s last blog post… Free Website Monitoring Tools to Monitor Website Uptime
This can be done with a plugin called WhyDoWork Adsense.
It enabled you to even set the number of days the ad should be live, etc.
Try it as it will be easy for people who don’t want to edit files.
@Pavan, @Prasanth, thanks for the plugin references… I prefer to keep the number of plugins (have just 12 of them) as low as possible and hence edit the code
[...] colour for the ads. Avoid yellow! It make you ads appear too artificial! Just be creative when you blend your adsense ads. Its all about colour [...]
Really great tips. Well, i think we can still reduce this code. But this helped me alot.
[...] This website has the instruction needed to insert the Google AdSense ad unit into the Wordpress Post Content page. [...]
Thanks a lot, i did many searchs to how to pud ads on each post and i didn’t know where to put the html code in php files, so your post has really helped me! Thanks! <333
Before I start jumping out of excitement, does this work with cache plug-ins like W3 Total Cache?
I’ve tried so many hacks including Who sees ads but none of them work with cache plug-ins
@Udegbunam, the script should work independant of the cache plugins because the ad block is rendered via javascript and not HTML.
Thanks a lot. I’m going to try implementing it on my blog ASAP. Expect feedback soon
I just tried the first code on my blog single.php before
and it tends to freeze page load. That ’s the post no longer loads when it gets to the adsense hack. Would you happen to know any reason for this occurring?I’d very much appreciate it if you could share a text file containing the code in the first image with us Thanks
Did you mean the code to show AdSense for 7 days or older posts? It’s available at the following URL (there’s a mistake in the image)
http://www.dollarshower.com/downloads/code-samples/single_post_adsense_conditional1.txt
That did it
. Seems to be working fine from my end. I set it to 14 days instead of 7. Don’t forget to change the image capture in the post as it has the wrong code.
P.S: Do you mind testing it for me from your end so I’ll be sure it works just as well as it did here? Thanks.
I’ve been testing the last code for showing two adsense ads and it has a bug. It tends to create two copies of the same blog post in one page.
For example at the top of the page is a post without adsense ads and at the end is another copy of that same post, on the same page but with adsense ads showing.
Can this be fixed?
I had tested this successfully on my ‘then’ theme and had absolutely no issues. If you know a little bit of php, you should be able to fix it easily
There may be possibly two content filters applied by you somehow.