• Ideas
    • Start Here
    • Team Management
    • Mindset
    • FBA
  • Build
    • Money Site Creation
    • WordPress
  • Monetize
    • Income Reports
  • Grow
    • Link Building
  • Tools
    • Tutorials
  • Services
  • About
    • Contact
  • Ideas
    • Start Here
    • Team Management
    • Mindset
    • FBA
  • Build
    • Money Site Creation
    • WordPress
  • Monetize
    • Income Reports
  • Grow
    • Link Building
  • Tools
    • Tutorials
  • Services
  • About
    • Contact
  • Ideas
    • Start Here
    • Team Management
    • Mindset
    • FBA
  • Build
    • Money Site Creation
    • WordPress
  • Monetize
    • Income Reports
  • Grow
    • Link Building
  • Tools
    • Tutorials
  • Services
  • About
    • Contact
  • Ideas
    • Start Here
    • Team Management
    • Mindset
    • FBA
  • Build
    • Money Site Creation
    • WordPress
  • Monetize
    • Income Reports
  • Grow
    • Link Building
  • Tools
    • Tutorials
  • Services
  • About
    • Contact
Display Ads

How to put ads in the middle of your post content

Jon March 15, 2012 12 Comments

I recently did a site design and build for a client who had a special request: He wanted to put ads in in the middle of his page content.  Generally my clients ask for ad blocks to be inserted, but they’ve always wanted them either at the top or at the bottom of a post or page.  Placing an ad block at the top or bottom of a page is easy, placing an ad block in the middle, automatically, is a bit more challenging and requires some special coding.

Put ads in the middle of your posts

How to put ads in the middle of your page content

While I’m sure the code I’m about to share with you would work with a few changes on any theme, the code as a whole is specific to the Thesis theme.

The easiest way to insert an ad block into the middle of your post or page is to just manually insert it.  This is easily done using the WordPress editor, and literally takes all of a few seconds.  That is not what we’re addressing here.  The code below will automatically insert an ad close to the middle of your article on every single post automatically.  Slick huh?

Code for ads in the middle of page content

Here’s the code:


/**
* Insert ads in the middle of a post - after 3rd paragraph
*/
function inject_ad_text_after_n_chars($content) {
$enable_length = 1500;
$after_character = 1000;
if (is_single() && (strlen($content) > $enable_length)) {
$before_content = substr($content, 0, $after_character);
$after_content = substr($content, $after_character);
$after_content = explode('</p>', $after_content);
array_splice($after_content, 1, 0, wp_ozh_wsa("AdSense-MidParagraph",false));
$after_content = implode('</p>', $after_content);
return $before_content . $after_content;
} else {
return $content;
}
}
add_filter('the_content', 'inject_ad_text_after_n_chars');

How the code works

  • The code first determines if the current page is a single post page or not, if not the ad won’t be shown.  It then determines if the post is more than $enable_length characters long, if not the ad won’t be shown.
  • The code then gets all of the post text up to position 1000 ($after_character) and all of the rest after this character position.  The first part is stored in before_content the rest in after_content.
  • The next line (explode(‘</p>’, $after_content)) basically breaks the after_content sections using the paragraph tags.
  • Next, an array_splice is done to place the ad code right after the first </p> tag found.  This is done so that the ad is placed between paragraphs – meaning the ad won’t be placed at exactly $after_character, but at the end of the paragraph end tag following $after_character.
  • Note the ad code is: wp_ozh_wsa(“AdSense-MidParagraph”,false).  This particular example uses a plugin called Who Sees Ads.
  • The $before_content is then concatenated with $after_content and returned.
The code above should be placed into your Thesis custom_functions.php file and Who See Ads configured to include the ad code you want to show.  This particular strategy of placing ads inside of your content is particularly effective with Google Adsense.
 
Let me know how this works for you and if you have any questions or problems using the code by adding a comment below!

Photo by: christopher.woo

290
2732 Views
AboutJon Gillham
I am a husband, father of 3, engineer and a huge fan of developing systems to build useful and profitable businesses (mostly online). The reason I build online businesses is to provide financial independence for my family so I can spend time outside skiing and biking with my them.
Jon Gillham, Online Entrepreneur
In Socials:
PrevMonetize your blog with AdsenseSeptember 12, 2011
Why is my blog slow? 10 possible reasonsJune 28, 2012Next

Related Posts

Display Ads

Advertising on my blog – When should you start

One of the more common group of questions I get on a daily basis from readers and...

Jon May 5, 2011
Display Ads

Monetize your blog with Adsense

If you’ve been following my Start a money making blog series, together we have:...

Jon September 12, 2011

Comments (12)

  1. Matt Jabs
    May 31, 2012

    Hey Larry, I’m trying to implement this code on http://www.diyNatural.com. It’s working, but only in some posts, even if they’re long enough. So I tried taking out the article length variable and even the “else” at the end but still only have it showing up in some of the content. I’m having trouble nailing down why it doesn’t work on some articles.

    Here is the code I have (but it happens even if I copy and paste code as is):
    /* Insert ads in the middle of a post */
    function inject_ad_text_after_n_chars($content) {
    $after_character = 300;
    if (is_single()) {
    $before_content = substr($content, 0, $after_character);
    $after_content = substr($content, $after_character);
    $after_content = explode('', $after_content);
    array_splice($after_content, 1, 0, wp_ozh_wsa("diyN-LR-txt-img-top",false));
    $after_content = implode('', $after_content);
    return $before_content . $after_content;
    }
    }
    add_filter('the_content', 'inject_ad_text_after_n_chars');

    Reply
  2. Matt Jabs
    May 31, 2012

    Well, now I found this code from Girlie on the diyThemes forums. I tried it, but no ads show up when I try that (which seems to work for everyone else) so I don’t know if it’s the PHP call for Who Sees Ads that’s not working, but I don’t think so because it doesn’t work when I plug Adsense code in directly either. I think I might need someone to analyze my custom_functions.php to look for broken code, like an extra ?> or something. Know anyone? 😉

    Reply
    • Larry
      June 5, 2012

      Hmmm – I just might 😉 Got your email and looking into it. I’ll post an update here in case anyone else is having a similar issue.

      Reply
  3. nive
    July 9, 2012

    Does this code works for blogger posts?

    Reply
    • Larry
      July 9, 2012

      Hi Nive, no it wouldn’t. As written, it would only work on a WordPress.org blog running the Thesis theme.

      Reply
  4. Stories Trending Now
    September 5, 2012

    Thanks Bro for the Article,
    But Please can I add the Adsense at the center side area under the post title instead of it being at the left side area for blogspot blog

    Thanks.

    Reply
    • Larry
      September 6, 2012

      Sorry, but I’m not real familiar with blogspot. Maybe another reader can chime in.

      Reply
  5. Reyn
    December 13, 2012

    Is this working in Tumblr?

    Reply
    • Larry
      December 14, 2012

      This is specifically for WordPress. I’m not familiar with Tumblr, so I’m not sure if you can this or not.

      Reply
  6. Michael Belk
    February 22, 2013

    Thanks a lot Larry I am discovering some great content on your site. You are sharing some good ideas here. I wish I had met you before I spent all that money on coding.

    Thanks a lot.

    Reply
    • Larry
      February 23, 2013

      Thanks Michael. Glad you’re finding the content helpful.

      Reply
  7. Molly
    March 13, 2013

    This was so helpful! But I’m actually trying to insert a link to a random related post within the same category, not an ad, is there a way to do that? I’m trying to replace the code for the ad with the code for the post, but I’m pretty new to PHP and seem to keep getting the code wrong.

    Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts
  • How to sell direct ads using Ezoic and increase your ePMV
    How to sell direct ads using Ezoic and increase your ePMV
    January 22, 2023
  • Case Study – Article to Video by Ezoic and Its Impact  
    Case Study – Article to Video by Ezoic and Its Impact  
    October 28, 2022
  • 6 Great Ways To Earn Money From Google [Updated in 2021]
    6 Great Ways To Earn Money From Google [Updated in 2021]
    January 1, 2021
  • Rewarding You For Seeing Ads – BLADE.Software Ad Block Extension
    Rewarding You For Seeing Ads – BLADE.Software Ad Block Extension
    April 1, 2019

Services

  1. BrandBuilders.io
  2. LightningRank.com
  3. Speedy.Site
  4. SiteBuddy.io
  5. MotionInvest.com
  6. Originality.AI
motioninvest.com
brandbuilders.io

Engineering Your Financial Freedom with Online Business   Sharing everything (except my passwords) as I repeatedly build income streams from online businesses
Contacts
If you have any questions about this site or the internet marketing tutorials I have here please don't hesitate to contact me…
Email Me – Jon Gillham at…
jon (at) authoritywebsiteincome (dot) com
Services
MotionInvest.com
ContentRefined.com
SiteBuddy.io
BrandBuilders.io
LightningRank.com

Privacy Policy | Terms & Conditions

Copyright © 2021 WebsiteIncome.com All Rights Reserved.