Subscribe via

How to Add Ads Inside Your Post Content

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.

That’s it! Enjoy!

Save and Share
StumbleUpon
Reddit

14 Responses to “How to Add Ads Inside Your Post Content”

[go to last comment]
  1. Rakesh Narang

    dude will the ad look like your cup of coffee thing

  2. Thaya Kareeson

    Hehe…the ad can be whatever you want. Just code it.

  3. huntsville pr

    Here is another way of doing it if you do not understand functions http://www.huntsvillepr.com/how-to-automatically-…

  4. James Delnort

    Your plugins are very helpful. Thank you!

  5. Thaya Kareeson

    You're very welcome!

  6. Jamesstevn

    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

  7. Michael Garmahis

    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?

  8. Thaya Kareeson

    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.

  9. Hugo

    That just nailed it! Thanks!

  10. Abhishek

    Great post! One question though. Is there anyway that the ad can be wrapped with the content ?

    -Abhi

  11. dino

    Sounds like a great plug in! Will certainly try it.

  12. bharat

    A post for beginners like this, helps a lot in term of monetizing their own blog!
    Thanks for sharing it mate.

  13. Praveen Rajarao

    Awesome info, thanks for sharing.

  14. Anderson

    interesting, but it can break html tags like <img src=""> or etc, so take care when using it.

[go to first comment]

Leave a Reply