How to Insert Ads In Between Content In WordPress, Without Any Plugin

If you run a blog website, most probably you will be inserting ads in your blog to generate some income. In WordPress (self-hosted), you can easily add ads to the sidebar by inserting the ad code to a text widget in the Widget section. The difficult part is to add ads to the content, particularly in-between the content. While there are some plugins that allow you to add shortcode to the content, they are not feasible if your blog already have thousands of article, unless you are willing to go back to insert the shortcode in every single article. In this article, we will show you a simple way where you can easily insert ad (or any other stuff) in-between your content without using any plugin.

Note: This method requires some meddling with the code. If you are not comfortable dealing with the code, don’t attempt it. You might also want to create a staging site of your blog and test this method on the staging site first before making it live.

Here is the method:

1. In your theme folder, open the functions.php file with a text editor.

2. Insert the following lines to the end of the file, before the “?>” tag

Remember to change the “insert_ad_code_here” string to the actual ad code. Also, if there is any instance of single quote (‘) in your ad code, you have to add a \ before it. For example, if your ad code is something like:

You have to make them all into one line and insert a \ before the single quote:

What we are doing with this function is to take the content for each post and break it up into each paragraph. We then detect whether the third paragraph exists. If yes, we insert the ad code to the end of the paragraph. Lastly, we insert back the starting paragraph tag to each section and glue them back into a complete article.

Things you can change here include:

1. The paragraph to insert the ad tag. The default in the above code is the third paragraph. You can change to the second paragraph by changing all instances of $content_block[2] to $content_block[1].

Note: The count of the array starts from 0, so $content_block[2] means the third paragraph instead of the second.

2. The tag to break the article. I used the paragraph tag <p> to break the article. You can use <h2> or <h3> tag instead as the marker.

3. The ad code. You don’t necessarily have to insert ad in-between the content. You can insert newsletter subscription form or any other stuff that you deem appropriate for your site.

Lastly, don’t forget to save the file and upload it to the server. As I mentioned earlier, it is best to test this on a staging site first before making it live.

9 comments

  1. Thanks! Have been hunting for this the whole day. Tried it and it works like a charm. Just one question. 

    Is there any way to add more than one ad unit? Say one after the 2 paragraph and another after the fourth. 

  2. Been using this for a while and it has worked great :)

    I had a new author join me and he used some css in his tag, like . Took me all day long to figure out why the ad insert code was working for me and not for him! Finally realized looking at the code wasn’t getting me anywhere and I looked at the source code of his post…. aha!

    So I switched to the using the end tag , since that is not likely to be changed.

    However, my ad code is surrounded by a div, so now I have a div inside a paragraph. Instead of in-between p tags, it falls just before the end p tag. It works, from a browser formatting standpoint, but it’s not really supposed to be that way.

    Do you have any suggestions for handling this with people using css in their p tags, or a way that using the end p tag would work with the div issue? I’ve got more authors coming on board that may do the same thing without realizing it, they just think they are formatting their post, they don’t know html, so the editor is making this changes.

    Thanks, Chris

    • opps, those blank spots/line breaks are example paragraph html tags that the comment thing didn’t like :)

    • To insert ad at the start of the post, you just modify the code:

      add_filter(‘the_content’, ‘mte_add_incontent_ad’);
      function mte_add_incontent_ad($content)
      { if(is_single()){
      $content_ad = ‘insert_ad_code_here’;
      $content = $content_ad.$content;
      }
      return $content;
      }

  3. Hi,
    You say to replace the “single quote (‘)” with a “\”, If the ads that I will put is adsense ads, does this not violate the google TOS? Btw Thank you, this article was helpful.

    • Nope, you are not violating the tos. The \ character is an escape character in php so the quote will appear as quote in the html. If you view the source code on your front page, you will find that the ad sense code is not altered at all.

Comments are closed.

Sponsored Stories