How to put ads in the middle of your post content
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.
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.
Photo by: christopher.woo
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');
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? 😉
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.
Does this code works for blogger posts?
Hi Nive, no it wouldn’t. As written, it would only work on a WordPress.org blog running the Thesis theme.
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.
Sorry, but I’m not real familiar with blogspot. Maybe another reader can chime in.
Is this working in Tumblr?
This is specifically for WordPress. I’m not familiar with Tumblr, so I’m not sure if you can this or not.
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.
Thanks Michael. Glad you’re finding the content helpful.
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.