WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
  • Deals
  • Glossary
  • Videos
  • Products
X
☰
Beginner's Guide for WordPress / Start your WordPress Blog in minutes
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

WPBeginner» Blog» Tutorials» How to Insert Ads within your Post Content in WordPress

How to Insert Ads within your Post Content in WordPress

Last updated on May 2nd, 2014 by Editorial Staff
721 Shares
Share
Tweet
Share
Special WordPress Hosting offer for WPBeginner Readers
How to Insert Ads within your Post Content in WordPress

Have you seen blogs that has ads inside their post content? These ads are either after the first paragraph or the second paragraph in most cases. Most beginners wonder whether these sites insert ads manually when they write the content, or if there is a special code for this. In this article, we will show you how to insert ads within your post content after specific paragraphs.

Often when beginners want to include ads in their post, they either add the code manually which is extremely inefficient specially if you have to change advertisers, or they insert ads above or below their post using one of the ad management plugins.

Well, we’ve been adding ads in List25 after first paragraphs, and a lot of users asked about it, so here’s the easiest way to do it.

Simply install and activate the plugin we created called Insert Post Ads. Upon activation, the plugin will add a new menu item called Post Adverts.

Click on Post Adverts » Add New. On the next screen, simply add the name of your ad for identification purposes, enter the ad code itself, and select the paragraph you want to display the ad code after. Once done simply click publish.

Insert Post Ads

Next you want to go to Post Adverts » Settings to select which post types you want to show your ads on such as posts, pages, and custom post types.

Now if you don’t like using the plugin, and want to do it the code way, then follow the directions below.

Open your theme’s functions.php or a site-specific plugin file and paste the following code:

<?php
 
//Insert ads after second paragraph of single post content.

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {
	
	$ad_code = '<div>Ads code goes here</div>';

	if ( is_single() && ! is_admin() ) {
		return prefix_insert_after_paragraph( $ad_code, 2, $content );
	}
	
	return $content;
}
 
// Parent Function that makes the magic happen
 
function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
	$closing_p = '</p>';
	$paragraphs = explode( $closing_p, $content );
	foreach ($paragraphs as $index => $paragraph) {

		if ( trim( $paragraph ) ) {
			$paragraphs[$index] .= $closing_p;
		}

		if ( $paragraph_id == $index + 1 ) {
			$paragraphs[$index] .= $insertion;
		}
	}
	
	return implode( '', $paragraphs );
}

To add your ad code, simply edit $ad_code value where it says “Ad code goes here” on line 9. Once you do that, you are done. To change the paragraph number, simply change the number 2 to another paragraph number on line 12.

We hope that this article helped you insert ads within your post content in WordPress.

If you liked this article, then please consider subscribing to our YouTube channel and join us on Twitter.

Thanks to @GaryJ for improving the code that we had.

721 Shares
Share
Tweet
Share
Popular on WPBeginner Right Now!
  • Google Analytics in WordPress

    How to Install Google Analytics in WordPress for Beginners

  • Why Build Your Email List Today

    Revealed: Why Building Your Email List is so Important Today!

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

  • Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

The Ultimate WordPress Toolkit

161 Comments

Leave a Reply
  1. André says:
    Jun 6, 2015 at 6:41 am

    How add in last paragraph?

    Reply
  2. Shubhi says:
    Apr 24, 2015 at 3:32 am

    Gr8 post and correct code. It worked smoothly…:)

    Reply
  3. Lyke Basilio says:
    Apr 16, 2015 at 9:50 pm

    Hi,

    I just added it to my personal blog and it works perfectly!

    Thanks,

    Reply
  4. Ashok Kumar says:
    Mar 17, 2015 at 6:05 pm

    Hi,

    I added an ad just fine before my post. However, when I tried removing it, the blank white space remains. Please let me know how to fix this, my site looks terrible and i’m at my wits’ end!

    Thanks,
    Ashok

    Reply
    • Ashok Kumar says:
      Mar 19, 2015 at 12:01 am

      Hi,

      I’ve been trying to fix this for 2 days to no avail. Could someone help?

      Thanks

      Reply
      • helper says:
        Apr 12, 2015 at 6:10 am

        fix drawAdsPlace to return the content, add simple-ads-manager.php (somewhere in the class):

        function get_drawAdsPlace($args = null, $codes = false) {
        global $samObject;

        if(is_null($args)) return ”;
        if(is_object($samObject)) return $samObject->buildAd($args, $codes);
        else return ”;
        }

        then use get_drawAdsPlace instead drawAdsPlace. (Updating plugin will kill this)

        Reply
  5. WPBeginner Staff says:
    Mar 6, 2015 at 11:12 pm

    This plugin allows you to add after the first paragraph, after the 9th paragraph, etc.

    You can specify that in the settings of each ad.

    Reply
  6. Zaman says:
    Mar 5, 2015 at 11:44 pm

    I have noticed that there are around three ads in each post in list25 with one ad after every the first post while two other ads randomly between the images.

    So, could you please inform how can it be achieved? Are you using the insert post ads or some manual coding for the second and third ads. If so, what is the manual code and where are you inserting it.

    Reply
  7. Annastasia Taylor says:
    Jan 27, 2015 at 1:03 pm

    Hi,
    “Insert Post Ads” plugin setting doesn’t work for me, when i click on settings-nothing displaying.

    Code works fine, Thanks!

    So my question is, can i repeat this action? I mean if i can insert the ads after paragraph 1 and 2.

    I tried to repeat the code, but doesn’t work : “Fatal error: Cannot redeclare prefix_insert_post_ads()…..”

    Please help

    Thanks in advance

    Reply
  8. Jorge Pinto says:
    Jan 23, 2015 at 6:27 pm

    Hi, its possible to use the above code with this one integrated?

    //////////////////here mobile ad

    ////////////////// here desktop ad

    Reply
    • Adjie Purbojati says:
      Jun 21, 2015 at 1:05 am

      Yes, u can using wp-is-mobile

      Reply
  9. Christian Rauchenwald says:
    Jan 13, 2015 at 5:23 am

    Great plugin BUT I’m missing one very important thing -> if I create 4 ads to be displayed after paragrahp two I should get the option to show one of them randomly…

    It would also be great to be able to define “categories” for banner, so that a poker related banner doesn’t show up in my day trading section.

    Reply
  10. Arsie says:
    Jan 13, 2015 at 3:33 am

    Hi,

    For multi-author WP-blog who does not want to use plugins, can you recommend the code that I can put in the functions.php?

    Reply
  11. Resham Panth says:
    Nov 19, 2014 at 4:07 am

    Hey I lke your plugin and I am using it on my blog. I would like to know how can I align my ad to right side so that the content appears on left ?

    Reply
  12. Dok Simon says:
    Oct 29, 2014 at 12:38 pm

    Hello !!

    Is it also possible to allow Authors to show Their Ads only on their contents on the blog ??

    Reply
  13. Salud Casera says:
    Oct 25, 2014 at 8:52 pm

    This one really worked for me. I removed the CSS and added some inline css and got the ad before the content, float left, etc. and it ended up exactly as I wanted it.

    You can see it here :

    Thanks for sharing.

    Reply
  14. Jorge says:
    Oct 25, 2014 at 10:42 am

    Hi guys, where can i change the css of this div:

    ?? Thank you

    Reply
  15. Ben Daggers says:
    Oct 13, 2014 at 6:38 am

    Quick question, what if I assigned “5” as the number of paragraph for the adsense code to appear, but the number of paragraphs in my post is only four. What will happen? Will it still work?

    Reply
  16. Amanda says:
    Oct 12, 2014 at 1:08 pm

    disregard to text part above an ad, I figured that out :)

    Reply
  17. Amanda says:
    Oct 12, 2014 at 12:54 pm

    I have 2 qusestions:

    1. Is there a way to add text above an ad? For example, amazon affiliates has a banner for $3 per sign up, but they are also doing a contest in the same signup where users can win $500, I’d like to ad a note above the referral banner for sign up about the contest, is there away to do that?

    2. How can I had this to the front page?

    Reply
  18. Belinda says:
    Sep 22, 2014 at 10:22 pm

    Great tutorial, thanks. Is there a way for me to had the ads also show up on my homepage? I have a sticky post at the top and then my posts show up after that but this version of the posts have no ads. So ads only appear if the reader actually clicks through on a post to the individual post URL. Would be great if the ads would show also on the homepage.

    Reply
  19. steve says:
    Sep 4, 2014 at 11:29 am

    What impact does this have on SEO considering there will be links to adverts within the content of the page?

    Reply
  20. Aadil says:
    Aug 17, 2014 at 7:53 am

    Hello Wpbeginner,
    Really a good explanation. Really like it. However I have one question, would be really grateful if you could help me. If we use the code way,Is there a way to exclude some pages like- about, contact us etc where we dont want want the ads to appear.

    Reply
  21. Antonios says:
    Jul 27, 2014 at 10:00 am

    With this plugin the ads placed in a post appear in all the other posts. How can I place an ad in one specific post, and that it won’t appear on other posts?

    I have posts related to specific services, products, and topics. I need to be able to place ads related to the specific post, and that won’t appear on other posts, because ads won’t be related to those other posts.

    Reply
  22. tigaruk says:
    Jul 1, 2014 at 3:36 pm

    How do i get it to only appear on posts pages using the code above, not the plugin. Currently its also appearing on my product pages.

    Reply
  23. dan says:
    Jun 27, 2014 at 6:18 am

    Ads don t appear in posts before the plugin was instaled?

    Reply
  24. Dan Sz. says:
    Jun 23, 2014 at 2:44 pm

    Is there a way to get this to work within a custom loop? For example, when showing the full content of a featured post on the home page.

    Reply
  25. kristarella says:
    Jun 20, 2014 at 5:12 am

    Thanks for this. I would recommend tweaking the code slightly to add the insert at the end of a post if it is shorter than the specified number of paragraphs. This can be done with:

    $num = count($paragraphs);
    if ($num == 0 || $num < $paragraph_id) {
    return $content.$insertion;
    }

    Reply
  26. verdipratama says:
    Jun 19, 2014 at 2:24 am

    Really helpful, thanks :)

    Reply
  27. Will Nicholls says:
    Jun 9, 2014 at 5:37 am

    I would like to use this put wrap the text around it. How can I do this ?

    Reply
    • Chris says:
      Jun 14, 2014 at 1:37 pm

      Seems like this would be some simple css to change the appearance. Do you have an example I could see online?

      Reply
  28. Karan Singh Chauhan says:
    Jun 9, 2014 at 1:48 am

    Great way to insert ads.

    Reply
  29. Brandon says:
    Jun 3, 2014 at 2:07 pm

    Is there a way I can do this but have it on last paragraph? I want to insert at bottom right of every post’s content and have text wrapped using div align=”right” which I can see in your code I can change the div ad container, but I want it on bottom. Thanks!

    Reply
  30. WPBeginner Staff says:
    May 28, 2014 at 7:49 pm

    Hi Josh,

    We have released a new version of Insert Post Ads. Please give it a try.

    Thanks

    Reply
    • Josh M says:
      Jun 7, 2014 at 2:09 pm

      Very nice! Great updates Team WPB!!

      Reply
  31. Magic says:
    May 24, 2014 at 9:53 am

    Thanks for the tutorial. What would the functions.php code be if I wanted the ad to show as the second paragraph up from the “bottom” of the page (not the second paragraph from the top of the page)?

    Reply
  32. Margaret Anne @ Natural Chow says:
    May 22, 2014 at 6:15 pm

    What do I do if there is only a blank space showing up where the ad is supposed to be? I don’t have an ad-blocking software on my computer so am I doing something wrong?

    Reply
  33. WPBeginner Staff says:
    May 22, 2014 at 3:57 pm

    Josh, thanks for the feedback. We have forwarded your suggestions to our development team.

    Reply
  34. Josh M says:
    May 20, 2014 at 1:23 pm

    Hi, great, simple plugin. Just wanted to make two suggestions.

    I would recommend removing the hard-coded styling for the div or add a css box in settings, so it can be positioned easier. For those wondering, remove this in insert-post-ads.php…

    style=”clear:both;float:left;width:100%;margin:0 0 20px 0;”

    A simple option (checkbox?) on the post/page to “exclude content ads” would be great as well.

    Reply
  35. St3elSh0t says:
    May 17, 2014 at 3:13 pm

    Hi Syed, could you please tell me how to add 2 ads after 1st paragraph so that there is one ad then a tab then another ad code?

    Reply
    • WPBeginner Staff says:
      May 19, 2014 at 7:06 am

      Unfortunately, currently it is not possible.

      Reply
    • Brandon says:
      Jun 3, 2014 at 2:13 pm

      You could use the code to add to functions.php and instead of using ad code here … you could do first ad code here second ad code here

      Then edit style sheet to set sizes and alignment for .maincontainer
      .firstad
      .secondad

      Reply
  36. Andor Nagy says:
    May 14, 2014 at 9:57 am

    Hi, Nice tutorial. Is there any way to add more then one ad? Like 1 after 2nd paragraph, 1 after 3rd and so on

    Reply
  37. ETphonehome says:
    May 14, 2014 at 9:30 am

    I’ve installed the plugin and it shows all in the preview but the ads are not shown when I publish the post. What did I do wrong? Thanks.

    Reply
  38. Crispylogs says:
    May 13, 2014 at 9:44 am

    did not work for me :(

    Reply
  39. WPBeginner Staff says:
    May 7, 2014 at 11:49 pm

    Those are hard coded in the template file single.php

    Reply
  40. Patrick says:
    May 7, 2014 at 7:53 am

    Can we post adds at paragraph 0 , i mean on top of a post ?

    Reply
    • WPBeginner Staff says:
      May 7, 2014 at 11:48 pm

      No you can not. The plugin only accepts values higher than 1.

      Reply
    • Brandon says:
      Jun 3, 2014 at 2:17 pm

      You can edit single.php and look for a line that says something like:

      <?php the_content('’); ?> or entry…

      and ad your code above it and save the file

      Reply
  41. WPBeginner Staff says:
    May 6, 2014 at 3:27 pm

    The ad code is provided by your advertisement provider like Adsense.

    Reply
    • David says:
      May 6, 2014 at 7:33 pm

      Not familiar with Adsense, could you provide an example? If I’m designing my own ad and its is a jpeg, what would the ad code be if the jpeg is called MattsAutoSales.jpeg ?? I’m used to working with print ads which usually just use the digital name for placement (MattsAutoSales.jpeg)
      Thanks for the help!

      Reply
  42. SarahZigler says:
    May 6, 2014 at 3:03 pm

    Really helpful, I’m waiting for my adsense got approved. And this is a good information for me to implement adsense. Thanks.

    Reply
  43. David says:
    May 6, 2014 at 12:31 pm

    Could you define “ad code” ? I’m missing how to create one.

    Reply
  44. WPBeginner Staff says:
    May 5, 2014 at 6:35 pm

    See our guide on How to Add Facebook Like Button in WordPress

    Reply
  45. Chetan Parate says:
    May 5, 2014 at 3:52 am

    How to add facebook like button on the WP page ??

    Reply
  46. Scott Buehler says:
    May 2, 2014 at 7:51 pm

    How are you guys inserting ads next to the featured image here on WPB?

    Reply
  47. WPBeginner Staff says:
    May 2, 2014 at 5:53 pm

    An existing plugin on your site may be conflicting. We recommend deactivating all plugins except insert post ads and then activate other plugins one by one until you figure out which plugin is causing the issue. Let us know.

    Reply
    • Benjamin says:
      May 2, 2014 at 6:05 pm

      I just tried and deactivated all plugins other than Insert Post Ads. Same result.

      You can see the problem here :

      http://kieloo.staging.wpengine.com/french-expressions/

      Reply
      • WPBeginner Staff says:
        May 2, 2014 at 7:50 pm

        Can you please switch to a default WordPress theme like Twenty Twelve or Twenty Thirteen and see if this solves the issue.

        Reply
        • Benjamin Houy says:
          May 3, 2014 at 3:10 am

          No. The issue remains. It may be a conflict with WP Engine’s configuration then.

  48. WPBeginner Staff says:
    May 2, 2014 at 5:09 pm

    Check out our guide on some other ad management plugins for WordPress.

    Reply
  49. Kim Whitley-Gaynor says:
    May 2, 2014 at 12:04 pm

    Very helpful information! Thank you very much. What plug-in do you recommend for inserting ads into a sidebar?

    Reply
  50. Benjamin Houy says:
    May 2, 2014 at 7:40 am

    Your plugin seems great. But the content of my posts doesn’t show anymore when I activate it.

    Reply
1 2 3 »

Leave a Reply Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Over 600,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
WPForms Logo
WPForms
Drag & Drop WordPress Form Builder Plugin. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 25 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2018 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2018)
    • Which is the Best WordPress Slider? Performance + Quality Compared
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • 5 Best VPN Services for WordPress Users (Compared)
    • HostGator Review - An Honest Look at Speed & Uptime (2018)
    • SiteGround Reviews from 1032 Users & Our Experts (2018)
    • Bluehost Review from Real Users + Performance Stats (2018)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Start a Podcast with WordPress (Step by Step)
    • How to Choose the Best Domain Name (8 Tips and Tools)
    • How to Setup a Professional Email Address with Google Apps and Gmail
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2018 – Step by Step Guide
Deals & Coupons (view all)
Soliloquy Logo
Soliloquy Coupon
Get 10% off Soliloquy, the best responsive WordPress slider plugin available in the market.
iPage Coupon
Get over 83% off on iPage web hosting and a free domain name.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • List25
  • Awesome Motive
  •  

Copyright © 2009 - 2018 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

WordPress hosting by HostGator | WordPress CDN by MaxCDN | WordPress Security by Sucuri.