As a webmaster, it is always good to experiment with advertisement placements to maximize your ad revenue. One possible ad placement is in the middle of your posts. One way to do this is to manually include the ad into every one of your post text (I’m not a big fan of this idea). The better way to do it is to modify your theme to automatically show this ad in your posts’ content. Since it’s a bit tricky, I’d like to share a quick tip on how to do this.
Copy/Paste
Copy and paste the following code into your theme’s functions.php:
function inject_ad_text_after_n_chars($content) { // only do this if post is longer than 1000 characters $enable_length = 1000; // insert after the first </p> after 500 characters $after_character = 500; 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); $text = ' <!-- PUT YOUR AD HERE --> '; array_splice($after_content, 1, 0, $text); $after_content = implode('</p>', $after_content); return $before_content . $after_content; } else { return $content; } } add_filter('the_content', 'inject_ad_text_after_n_chars'); |
Modify
- Make sure to replace “PUT YOUR AD HERE” with your advertisement code.
- $enable_length: Your ad will only be inserted if the post length exceeds this value.
- $after_character: Your ad will be inserted after the first </p> after this value.
commented on May 19th, 2010 at 5:36 am
dude will the ad look like your cup of coffee thing
commented on May 19th, 2010 at 5:40 am
Hehe…the ad can be whatever you want. Just code it.
commented on May 20th, 2010 at 1:35 pm
Here is another way of doing it if you do not understand functions http://www.huntsvillepr.com/how-to-automatically-…
commented on May 22nd, 2010 at 5:08 am
Your plugins are very helpful. Thank you!
commented on May 22nd, 2010 at 5:09 am
You're very welcome!
commented on July 2nd, 2010 at 8:35 am
Very good plugin. I am trying those. I have a question that, after add Ads inside my post content, will users feel to to be cheated?
And, will the clicks get a big increase?
Thanks
commented on July 9th, 2010 at 9:22 pm
Nice code, thanks!
If I want to display 2 ads: after first and third paragraph, should I just duplicate the code? If I don't care about characters how can I simplify it?
commented on July 21st, 2010 at 5:42 pm
The best solution requires a bit of coding knowledge. To keep it simple, it sounds like you should just duplicate the code and change inject_ad_text_after_n_chars() to inject_ad_text_after_n_chars_2() for the duplicate code.
commented on August 12th, 2010 at 5:14 pm
That just nailed it! Thanks!
commented on March 2nd, 2011 at 6:56 am
Great post! One question though. Is there anyway that the ad can be wrapped with the content ?
-Abhi
commented on March 13th, 2011 at 11:04 am
Sounds like a great plug in! Will certainly try it.
commented on March 21st, 2011 at 7:30 am
A post for beginners like this, helps a lot in term of monetizing their own blog!
Thanks for sharing it mate.
commented on June 9th, 2011 at 6:17 pm
Awesome info, thanks for sharing.
commented on December 11th, 2011 at 8:10 am
interesting, but it can break html tags like <img src=""> or etc, so take care when using it.