How to Add Adsense Ads in Middle of Blog Posts

By 28-12-2013   AdvertisingBloggingWordPress

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?

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.

24 comments on “How to Add Adsense Ads in Middle of Blog Posts

  1. Dean Saliba says:

    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. Sagar nandwani says:

    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?

    • Arslan | DailyHow2 says:

      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.

  3. Tarun Singh says:

    How to insert Adsense ads in blogger ? Please help.

    • P. Chandra says:

      You need to edit the blogger theme template and add adsense code directly where you want it.

      • ORMAXO says:

        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 ?

    • d says:

      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.

  4. Sunday says:

    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!

  5. Mark McDougall says:

    Thanks for this. I have been wondering how to do this for a long time. Might try to implement this on my blog once I’ve got my head around it!

  6. friv game says:

    thanks for sharing

  7. WindowsBoy says:

    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.

  8. Tahmid Hasan says:

    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

  9. valentines day 2014 says:

    thanks for sharing. Might try to implement this on my blog once I’ve got my head around it!

  10. james cameron says:

    amazing post thanks for sharing

Leave a Reply

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

*