How to Add Adsense Ads in Middle of Blog Posts

Can you display Adsense ads in middle of blog posts? There is no doubt that in-content ads encourage readers to click more, and the higher CTR can earns you more money. While it is easy to add Adsense ads below post titles or after post content, how can you add it in middle of articles?

Advertisements

Of course there are very powerful Adsense plugins that can let you perform custom ad placements almost anywhere you want. But I prefer to add code to functions.php and keep it simple, as much of the features provided by plugins might not be needed.

So how can you add your adsense like this?

adsense middle

Add Adsense in Middle of Post

Here is the code you need to add to the functions.php file of your WordPress theme. [Note it is a good idea to keep backup of functions.php file and any error can make your site unusable and you need to restore the file via FTP]

add_filter( 'the_content', 'qot' );
function qot( $content ) {
if( !is_singular() )
return $content;

$paragraphAfter = 5;
$content = explode ( "</p>", $content );
$new_content = '';
for ( $i = 0; $i < count ( $content ); $i ++ ) {
if ( $i == $paragraphAfter ) {
$new_content .= '<div class="middle-ads">';
$new_content .= 'ADSENSE CODE';
$new_content .= '</div>';
}
$new_content .= $content[$i] . "</p>";
}
return $new_content;
}

1. What this code does now is locate the paragraph after which you want to add the code. Right now we add the adlink code after the 5th paragraph. If you want the adds further down the page, you can change the value here from 5 to whatever you need.

$paragraphAfter = 5;

2. It is also important to locate the ads correctly. We have used the middle-ads class here to style the ads. You will typically need to add margins, padding or float elements to correctly position the ads. For example you can use

.middle-ads {margin: 10px 0;}

3. Remember to replace the ADSENSE CODE with the code for your account.

4. If you only want to add the code to single posts, change the is_singular (which will add ads to both posts and pages) to is_single.

The credit for the original code goes to Brad Dalton who posted the code on Github. We have modified it a little to suit our needs.

23 thoughts on “How to Add Adsense Ads in Middle of Blog Posts”

  1. Every time I mess with my Function.php file I end up breaking the theme. With that in mind I would suggest a plugin called Ad Injection, which can put ads at the top, middle and bottom of your posts and pages, and can also add an ad into your footer.

  2. Google will automatically inject in your posts up to 3 AdSense blocks. So the first 3 posts (in a top-down order) in your home page or archive pages will have ads.”

    I need this tutorial. Just home page + archive pages will have ads. But 9 post have on home page.

    Blogger default (first 1. post + adsense + 3 posts + adsense + 3 posts + adsense + 2 posts + navi

    But i want ;
    First 3 post + 728×15 adsense + 3 post + 728×15 adsense + 3 post + navigation

    But i cant do it :( Please help me.

    How to make it?

    1. Dude, you need to install custem-designed theme in wordpress in order to get this functionality. Or you can check and install premium theme which offers these feature to show adsense ads on homepage and other pages of your choice.

      1. but ? if i have more than 3 – 4 post on my home page of bloggr.com than i think it will show 3-4 ads simultanusly ? ….. it may comes under terms of violation ? plz suggest ?

    1. The solution for putting Adsense ads into blogger can work like this:
      http://devurls.co/2014/01/01/add-google-adsense-to-your-tumblr-posts-with-jquery/

      It involves jQuery.

  3. Although I would prefer using any Google Adsense Plugin that can easily do this task with just a few click, its still helpful to learn this manual procedure. After all, there are times the plugins may disappoint and this code or manual process would become very handy! Thanks for sharing this piece!

  4. Hi,
    Thanks for the handy guide. Is it possible to show “Advertisements” or “Sponsors” just above the ad? Also, it would be great if we could show ads only on pages with certain amount of words, say 900 words.

  5. I am very happy to know this. because I was trying to keep banner in middle of post of my blog site. I think that I can now to do it perfectly thank a lot. Also you can to post in my blog here is my blog site. please visit http://www.graphicalarm.com

  6. This is an amazing post, much appreciated.This is great example of providing something valuable to your readers.Actually I would except find a post like this to be in a book that is for sale.
    Thanks for the article.

  7. Good article but i prefer to use plugins for this kind of work because you can sometimes, accidently mess code up. And you wouldn’t know how to fix this. So i think it is better to use Quick Adsense plugin for adsense-related stuff.

  8. Hi
    This post is very nice. sir tell me more about Google Adsense . and how to improve our blog to increase traffic for Google Adsense. This is my blog check it and give me some tips.
    http://dressquery.blogspot.com/
    Thanks.

  9. Great information here, I have been an Adsense plublisher for years now, I did make some money in the process way back in 2007-2008. It’s really all about ad placement colors and for me it’s seems that text ads were converting better than picture ads.

    Thanks for the great read

Leave a Reply

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

CAPTCHA Image

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>