Inserts Ads After a Few Paragraphs

Advertising is a part of the modern life

Putting ads on your WordPress site is quite a personal choice: for some it will be one of the first things you do, while others will resist it for a while. After quite a while of refusing to put ads on my site, I have finally decided to put up some ads that advertise my own services, not least to educate my readers that I do offer things like WordPress hosting packages and WordPress consulting services.

I created a few ads in the standard 125×125 and 468×60 sizes. The intention was to put the 468×60 ads at the bottom of the article, and the 125×125 ads at the beginning of the article. However, after trying that, I didn’t like how an ad was the first thing your eyes see when trying to read an article – the emphasis of my site is on informative WordPress tutorials, not the ads. So I wanted to set about moving it down the page slightly.

This seemed like a simple idea, but there was more to it. The standard call for displaying your WordPress content (the_content) is just a single line that automatically pulls the entire post from the database from beginning to end, so inserting something in the middle of it is not as easy as it sounds.

However, you can use the following code in place of your content call to enable you to place your ad code (or any other type of code) after a set number of paragraphs. So, open up your theme editor and open single.php for editing and look for the following line:

Now you’ll want to replace that with the following code snippet. Don’t worry about it’s apparent complexity – there’s only two simple variables in there that you will need to change:

In this example, the ad will be displayed after the second paragraph. This is as a result of the $show_after_p = 2 line near the top of the snippet. You can change this to another value to suit your needs.

The other thing you will need to change is the ad code itself. Two thirds of the way down, you will see a spot that says YOUR AD CODE GOES HERE. This is where you should place your AdSense or other code.

Since I wanted to use ads that were advertising my own sites, I went with an internal ad control plugin. After reading an article on DataFeedr Random Ads v2, it sounded like a perfect fit for my needs. You can use it with ad publishers’ codes (like AdSense or BuySellAds) and also sprinkle in your own ads.

After a little more experience with ads, I now mostly use WP Ad Center, or OIOpublisher for managing my ads.

All I needed to do was copy the PHP template code that Random Ads provided me and slot it into the snippet above and you can see the results for yourself.

Have you ever considered putting ads on your site? Where do you place them? Is this sort of solution a welcome relief to you?

25 thoughts on “Inserts Ads After a Few Paragraphs”

  1. Bob says:

    This is absolutely genius! I know very little about the techy stuff of websites but I can certainly handle a cut’n’paste job!

    Previously on my blogs that carried adverts I had manually added snippets of the adsense code into the places where I wanted it to appear.

    1. Well, I try to make it as easy as possible! It certainly beats adding the code in manually. Cheers

  2. ziad says:

    This is quite the nifty trick! I use the Quick Adsense plugin that gives you a fair amount of flexibility in ad placement. But this works too. Thanks a lot.

    1. Yes, I used to use the Easy AdSense plugin which was also quite helpful, but I wanted to be able to specify where my ads show up.

  3. Peter says:

    It will automatic to insert the ads ? How about if I want to insert the ads above the first picture on the post ?

    1. Yes, it will insert the ads above whatever paragraph you specify. I don’t know off hand how to show an ad above the first picture in your post, unless you’re talking about the featured image, in which case you can just modify your theme where it calls your featured image.

  4. Kristina L. says:

    I know a lot of people who use Easy Adsense plugins, but the fact that you can control the placement of your adds seems to be more better choice. This sounds very simple to do, especially when you made the effort to present it in such an easy way, so it`s just left for us to copy-paste your examples.
    Thanks a lot for sharing this.

    1. Hi Kristina,

      Yeah Easy AdSene is pretty good and at least has the option to insert ads halfway down your posts, but this is certainly more controlled.

      I’d be interested to hear if you tried it and if so, how easy it was.


  5. Unknown says:

    Hey Dave, thanks for doing this tutorial, i really wanted to do something like this but since i’m a complete php/wordpress noob.

    Thanks for the help,

    – Jenny.

    1. You’re welcome Jenny. Did you try it? Were you able to get it to work?

  6. John says:

    The “Quick Adsense” plugin supports this (and lots of options – after any paragraph you want, rotating ads…). It is very helpful to see the code to do these things manually (because sometimes that is best). But using plugins that are well done is so easy :-)

    One thing people have to remember any time they start editing the templates directly is to remember that they may need to do those again after updating (if the theme updates that file, your changes will be lost…).

    1. Agreed. The best way to do it would be to create a function and put it in your functionality plugin to avoid changes being overwritten by theme upgrades. I’ve tried to write my more recent tutorials in this fashion, to avoid editing theme files at all costs.

    2. Holger says:

      Thats why you should always build a child theme when you modify your wordpress theme. This avoids to loose your changes when you update the main theme core files. It takes just seconds to build the child theme but will save you lots of trouble ;)

  7. Scott says:

    I noticed that after implementing this code, the function stopped working. must be added to the code?

  8. Scott says:

    Apparently, the code was striped, I’ll try again…

    The ‘next page’ function stopped working after implementing this code.
    Must wp_link_pages be inserted into the code?

    1. wp_link_pages should be inserted in any well-coded theme. Some plugins or snippets will break the pagination of (this perhaps an example), and it is usually because wp_link_pages is not present in single.php, so make sure to put it into the theme and that will hopefully resolve it.

      1. Scott says:

        Thanks for the reply.
        As stated, w/o the above code, pagination works; wp_link_pages is in the theme, outside of the above code. Pardon if I was unclear…I need to now where and how to place wp_link_pages inside the above code to make that function work again.

        1. Ahh, then I did misunderstand. If the theme already includes wp_link_pages, then you don’t need to include it in the code above; it should work by itself. I’m surprised that the above code broke it. I’m not sure what the issue is I’m afraid.

  9. Holger says:

    Dave, isn’t der an else statement missing for the main if statement? I had no output on posts where paragraph count is less then show_after_p. And whats the reason for this main if statement at all? Don’t the p_count == $show_after_p if statement be enough to render the content without Ad when there are less paragraphs present? I am thinking about dropping the mein if statement.

    BTW. WordPress uses p tags in image captions as well. If you have an image with a caption in your content before the show_after_p paragraph, this is a problem. I took your code and modified it slightly (second code example):
    I used the clean opening p tags to avoid the caption p tags, which come with additional css class.

    In the third and fourth code example I add additional Adsense blocks at the post end and post middle depending on the overall post length.

  10. Michael says:

    Line 16 has an extra space < ? that needs to be removed.

    1. So it does. Thank you!

  11. Dave, your code was a life saver. I used your code to display meta data within a DIV element though :) Thank you.

  12. Brandon Hann says:

    This code is amazing!! However, I’m not too versed in PHP coding, so maybe you can help. My theme calls out the “Read More” link in a unique way so my php the_content code looks a little different.

    How do I get the code that is sitting in the usually blank area between the parentheses to translate into your code??! If I simply remove it, my site and individual articles work fine, but I no longer see the excerpt for the posts listed on my homepage…it just shows the whole article.

    If I use this line: $content = apply_filters(‘the_content’, get_the_content( __( ‘Read More…’, ‘cudazi’ ) ), $post->post_content);
    then I no long see parts of my content on the homepage…just the title and featured image. Any ideas?

    1. The read more link only shows when you’re not viewing the single post (i.e. on the home page or in the archives). So, this doesn’t apply to your situation, because it sounds like you’re trying to insert something in between a few posts on your front page, rather than in between paragraphs in an article

  13. Dale says:

    Great code, thanks. However you need to add an “else” clause for instances where the post contains less paragraphs than defined in “show_after_p”. In the given example I admit it’s unlikely but people may use this code to add adverts much further down the post.

    } else {

    would suffice – that way the post would still display even if it contained less paragraphs than defined in “show_after_p”.

Leave a Reply