WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All 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
277 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
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.

277 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • How to Start Your Own Podcast (Step by Step)

    How to Start Your Own Podcast (Step by Step)

  • How to Fix the Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

  • Google Analytics in WordPress

    How to Install Google Analytics in WordPress for Beginners

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

206 Comments

Leave a Reply
  1. Marvin says:
    Jul 13, 2020 at 8:04 am

    How can i make it to repeat (For Example. to repeat after paragraph 2.)?

    Reply
    • WPBeginner Support says:
      Jul 14, 2020 at 10:30 am

      We do not have a recommended method for having the code repeat at the moment.

      Reply
  2. Deepak says:
    Jun 16, 2020 at 2:00 am

    Fascinating. It is so easy. Thank you guys.

    Reply
    • WPBeginner Support says:
      Jun 16, 2020 at 9:10 am

      You’re welcome :)

      Reply
  3. Mi Monir says:
    May 24, 2020 at 10:16 am

    This is working. Thanks a lot.

    Reply
    • WPBeginner Support says:
      May 27, 2020 at 8:34 am

      You’re welcome :)

      Reply
  4. Oniel says:
    May 6, 2020 at 1:00 pm

    Can you please update the plugin to be able to add the code “before” the first paragraph instead of “after” the first paragraph?

    Reply
    • WPBeginner Support says:
      May 7, 2020 at 10:00 am

      The plugin does have the option to insert before your post content instead of after a paragraph number

      Reply
  5. Shannon says:
    Apr 14, 2020 at 11:26 am

    This is amazing, thank you so much for this. Do you know why my ad goes to the left of the post and not in the middle?

    Reply
    • WPBeginner Support says:
      Apr 15, 2020 at 8:37 am

      The most common reason would be if your theme has styling that is forcing that alignment.

      Reply
  6. Karthik says:
    Apr 9, 2020 at 8:09 am

    how much time take for adsense ad display on our site once we get approved after the code placed.

    Reply
    • WPBeginner Support says:
      Apr 9, 2020 at 9:13 am

      That would be a question for Adsense as they may not have ads they would currently offer for your site.

      Reply
  7. Sangam says:
    Oct 31, 2019 at 1:02 pm

    Thank you, with the help of this snippet I managed to show ad after each 300 words. I just added word count instead of paragraph.

    Reply
    • WPBeginner Support says:
      Nov 1, 2019 at 9:46 am

      Glad we could give you a starting point :)

      Reply
  8. Chango says:
    Oct 3, 2019 at 10:20 am

    Hello is that Insert posts ads plug in good? or does it crash the site or slow it down? I am nervous because 7 years ago I used a adsense plug in and the maker of the plug in was adding their ads on my website.

    Is this insert ads plug in good that you recommend. Does it mess with coding or slow down the site?

    thanks.

    Reply
    • WPBeginner Support says:
      Oct 4, 2019 at 9:44 am

      Unless you manually add problematic code, the insert post ads plugin shouldn’t harm your site’s speed.

      Reply
  9. Arkantos says:
    Oct 3, 2019 at 1:05 am

    Works great! But I noticed it also counts the paragraphs from an embedded tweet. Is it a way to avoid that and count only text paragraphs from the content/article?

    Reply
    • WPBeginner Support says:
      Oct 3, 2019 at 9:45 am

      Sadly, not at the moment.

      Reply
  10. Von says:
    Sep 20, 2019 at 12:46 pm

    Is there a wordpress tag to have a ‘content/block’ automatically injected into the middle of my blogs.

    I’m looking for documentation (I’d seen it before). I would appreciate any help here.

    Thank you.

    Reply
    • WPBeginner Support says:
      Sep 23, 2019 at 9:21 am

      For what it sounds like you’re wanting, you would want to use the recommendation from the article

      Reply
  11. Willys says:
    Sep 17, 2019 at 7:01 am

    Hi, I have just Signed up to Google adsense but I am having a problem of how to copy my Google adsense code to my website (WordPress). How can i do it?

    Reply
    • WPBeginner Support says:
      Sep 17, 2019 at 10:27 am

      For adding Adsense you would want to take a look at our article here: https://www.wpbeginner.com/beginners-guide/how-to-add-google-adsense-to-your-wordpress-site/

      Reply
  12. Sangam says:
    Aug 16, 2019 at 8:52 pm

    I am using this code snippet in my child theme function.php to make ads appear in 3rd paragraph. But I want it to repeat after each 3 or 4 paragraph. How it can be done?

    Reply
    • WPBeginner Support says:
      Aug 19, 2019 at 10:38 am

      As we state in the article, you would change the number in the code snippet to change where the ad appears.

      Reply
      • Sangam says:
        Aug 19, 2019 at 12:40 pm

        How to repeat? After 3rd/6th/9th/12nd etc?

        Reply
        • WPBeginner Support says:
          Aug 20, 2019 at 10:01 am

          If you wanted the ad to display multiple times then inside the if statement where it has: return prefix_insert_after_paragraph
          You would want a new line where you add a copy of the prefix_insert_after_paragraph with a different paragraph number

  13. Ahsan Ali says:
    Jun 17, 2019 at 3:19 pm

    How to insert ad after each & every 3rd paragraph in a single post?

    Kindly guide!

    Reply
    • WPBeginner Support says:
      Jun 18, 2019 at 10:28 am

      You would need to use the site-specific plugin method and change the 2 to a 3 on line 12

      Reply
  14. Peter says:
    Jun 12, 2019 at 2:26 pm

    Hi
    Only the code is showing on my post no images. The code is just lying there in the post, you won’t even notice it’s there
    What can I do to change this
    I need the banners showing in the post

    Reply
    • WPBeginner Support says:
      Jun 14, 2019 at 9:55 am

      You may want to ensure you added the code with all of it’s tags correctly for your ads and didn’t skip any tags.

      Reply
  15. Ngo says:
    Apr 10, 2019 at 7:03 pm

    Hello,

    Thank you for the manual code. It seems to be working for everyone but I just can’t get it to work on my site. My site is made up of only Custom Post Types. Is this why it is not working? Please what do I need to change in the code to make them display on my custom post types?

    Even ordinary widgets do not display on my site. Is there anything I can include in my functions.php to make changes display on my custom pages?

    Please help, I have tried every code on the internet and none of them works.

    Reply
    • WPBeginner Support says:
      Apr 11, 2019 at 10:54 am

      If you want to use the custom code on a custom post type and the method we have is not working, you could try changing is_single() to: is_singular( $post_types = ” )
      and place the name of your custom post type in the single quotes

      Reply
  16. Swagatam Innovations says:
    Feb 5, 2019 at 10:31 pm

    It worked like charm, thanks very much for this code!

    Reply
    • WPBeginner Support says:
      Feb 6, 2019 at 10:21 am

      Glad our article could help :)

      Reply
  17. Minh Hieu says:
    Dec 30, 2018 at 3:35 am

    Can you help me?

    How to insert ads before an ID element?

    Ex: i want to insert ads before

    Thanks you very much!

    Reply
  18. JOHN HOBBS says:
    Dec 26, 2018 at 7:55 pm

    Thanks for everything you provide. It’s truly appreciated. But question. This article reads

    Last updated on May 2nd, 2014

    at the top. Is the information presented still valid and up-to-date?

    You guys have been a tremendous help to me so I feel bad asking but I need to know.

    Again, thanks for the great resources you provide!

    John

    Reply
    • WPBeginner Support says:
      Dec 26, 2018 at 11:40 pm

      Hi John,

      The plugin mentioned in the article is still available and works great.

      Reply
  19. Tell.com.ng says:
    Sep 5, 2018 at 7:56 am

    Please I need a clearly defined way to add more than one ad after different paragraphs.

    Reply needed urgently please.
    Thanks!

    Reply
  20. Atul says:
    Aug 16, 2018 at 5:55 am

    i am using infinite scroll featured theme and post advert plugin for ads but ads show only on first post, not next post. how can i solve it. this plugin does not repeat ads between post.

    Reply
  21. Melissa says:
    Aug 13, 2018 at 5:47 pm

    This is by far the easiest plugin I’ve ever used! It works perfectly and looks great. Thank you so much! WPBeginner has been a huge help to me in starting my new blog! :)

    Reply
  22. rishabh shrivastava says:
    May 24, 2018 at 6:39 am

    It was helpful! Thanks

    Reply
  23. sandeep says:
    May 23, 2018 at 6:06 am

    thank you so much its working dude

    Reply
  24. Hoàng Trí says:
    Mar 29, 2018 at 11:35 pm

    So I Want to insert ads by tag (Like Gym, Fitness) only two tag will show ads, and other will not. How i can do it ?

    Reply
  25. Bob says:
    Feb 12, 2018 at 9:15 pm

    How can I place an ad before the first paragraph..underneath the single post image? When i set the number to”0″…nothing happens?

    Reply
  26. Qasim Iqbal says:
    Feb 7, 2018 at 8:57 am

    Hey, If I added the code using Post advert, would it be on every post? right?
    If not then tell me, I want that

    Reply
  27. Bright Joe says:
    Jan 12, 2018 at 4:57 am

    Thank you so much for the code for the functions.php. This just lets me avoid so many unnecessary plugins.

    Thanks again!

    Reply
  28. Martijn says:
    Jan 7, 2018 at 11:10 am

    Hi,

    Thanks for the code!

    How can I exclude certain posts?

    Greetings,
    Martijn

    Reply
  29. David says:
    Dec 16, 2017 at 12:08 pm

    Was sincerely helpful to start off for my presentation at Kampala WordCamp 2017.
    Really incredible help from you.

    Reply
  30. Segun says:
    Dec 13, 2017 at 8:04 am

    how can i insert HTML code in my ad space?

    Reply
  31. Adem says:
    Dec 3, 2017 at 6:19 pm

    for more as one ad, change to this:

    $ad_code = ‘Ads code goes here’;

    $ad_code2 = $ad_code;

    if ( is_single() && ! is_admin() ) {
    $content = prefix_insert_after_paragraph( $ad_code, 2, $content );
    $content = prefix_insert_after_paragraph( $ad_code2, 8, $content );
    return $content;
    }
    return $content;
    }

    Thanks!

    Reply
    • Bright Joe says:
      Jan 12, 2018 at 5:02 am

      Thank you so much!

      Reply
      • Akios says:
        Sep 26, 2018 at 2:07 am

        Don’t know how to insert it

        Reply
    • Sunil Kumar says:
      Oct 9, 2019 at 8:39 pm

      Thanks! It worked.

      Reply
  32. Shehab says:
    Nov 30, 2017 at 1:41 pm

    I want to show two ads on my post. How can I do it?
    That process you showed, here I can see only one ad on every post. but I want to show two ads in every post

    Reply
  33. shuseel says:
    Nov 3, 2017 at 3:42 am

    I want to implement ads code other than Adsence code using JavaScript code.

    The above code does not work when I have replaced JavaScript code on Ads code goes here.

    Please suggest me how to implement
    on the place of text.

    Reply
  34. phil says:
    Oct 11, 2017 at 1:43 am

    Hi,

    Congratulation for this function.php code ! Works well

    I ve a question :

    How can i do for do not display adds on specific posts or pages ?

    Many thx
    Regards
    Phil

    Reply
  35. Andrea says:
    Oct 8, 2017 at 2:14 pm

    Nice code! How insert only for a certain author post?

    Reply
  36. Aditya says:
    Sep 30, 2017 at 2:24 pm

    I am using Admiral theme and I want to display ads above the thumbnail of every post. What can I do ?

    Reply
  37. Shubhamk says:
    Sep 19, 2017 at 5:25 am

    I installed the plugin, I created my ads in the post but it appears at the bottom, not at the start.

    Reply
  38. Sally says:
    Aug 23, 2017 at 11:13 pm

    Hi, I installed the plugin on my wp site and followed the instructions – post ad and edit settings to show ads on posts. But the ads don’t show on my posts. Do you know why? thanks in advance!

    Reply
  39. Gail says:
    Aug 9, 2017 at 2:01 pm

    Have just started using the plug-in and it works great!! A question: I now want to insert a new ad. Do I have to trash the old one and then create a new one? Or can I reserve the old one for a future use? Seems like I do have to trash it to make room for the new one. Thanks.

    Reply
  40. Percival says:
    Jul 1, 2017 at 9:04 pm

    I am wondering if it’s possible to insert a widget, rather than $ad_code = ‘Ads code goes here’;

    Is this possible? I can’t seem to implement the syntax correctly, as inserting a widget seems not to respect the paragraph numbering, and randomly inserts a “1” into the text:

    $ad_code .= dynamic_sidebar(‘test’);

    Reply
    • John says:
      May 28, 2018 at 7:40 am

      Percival, did you figure out a solution? Trying to do something similar.

      Reply
  41. Morgan says:
    Jun 28, 2017 at 1:17 am

    Where do I get a code for an ad .?

    Reply
    • hassan says:
      Aug 19, 2017 at 12:15 pm

      did you mean code for ads?………
      you have to visit your ads network be it google adsense or for your ads code then insert it between insert here……….

      Reply
    • Stephen says:
      Oct 20, 2017 at 7:20 pm

      You have to make a Google Adsense account and make an ad within that. Then, you take the code they give you for the newly created ad and insert it there.

      Reply
  42. Jagadish Prasad B says:
    Jun 26, 2017 at 12:10 pm

    I want to put my ads in the middle of the content by using an editor. Pleas e suggest me how to add the code in the editor. Thanks for the support.

    Reply
    • Ryan S says:
      Jul 9, 2017 at 7:12 am

      Creating shortcode is the best fit on your end, so first, you need to create Shortcode ads and then use that shortcode in your article/post.

      Ryan S

      Reply
  43. hsabarp says:
    Jun 5, 2017 at 6:09 pm

    thanks
    this is the what i was searching for…….

    Reply
  44. Vỹ Spirit says:
    Apr 25, 2017 at 7:36 am

    Hello, After I add the above code to the function.php file, my article does not display ads, instead, and it’s adsbygoogle text …. How do I fix it?

    Reply
    • hassan says:
      Aug 19, 2017 at 12:18 pm

      i did mine some minutes ago and it work well try to remove the <?php if your functon.php already have it…….

      Reply
  45. Enrique Grobbelaar says:
    Apr 18, 2017 at 9:00 am

    This app seems to work for everyone, except me. :-( I have followed all the steps, said it should display after paragraph 3. Now there is a large white block on all posts below paragraph 3, but no ad.

    Does somebody perhaps know what I am doing wrong?

    (Using Google Adsense)

    Reply
    • Enrique Grobbelaar says:
      Apr 18, 2017 at 9:14 am

      Never mind. It is working now. :-)

      Reply
  46. Great says:
    Apr 12, 2017 at 8:33 am

    Thanks you for the tutorials, I used the post ad on my site is working perfectly well but can not open in a new window. Pls how do I set the post ad banners to open in a new window?

    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 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
WP Mail SMTP logo
WP Mail SMTP
Fix WordPress email delivery issues. #1 SMTP 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]
    • 30 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 2020 (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 (2020)
    • How to Choose the Best Domain Registrar (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
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2020)
    • SiteGround Reviews from 4196 Users & Our Experts (2020)
    • Bluehost Review from Real Users + Performance Stats (2020)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • 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 2020 – Step by Step Guide
Deals & Coupons (view all)
The Constant Contact logo
Constant Contact Coupon
Get 20% OFF Constant Contact, one of the best email marketing service for small businesses.
Churnly Coupon
Get 30% OFF on Churnly WordPress automated churn-busting plugin.
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).
Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon

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

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress CDN by MaxCDN | WordPress Security by Sucuri.