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!
  • Google Analytics in WordPress

    How to Install Google Analytics in WordPress for Beginners

  • How to Start Your Own Podcast (Step by Step)

    How to Start Your Own Podcast (Step by Step)

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

  • Revealed: Why Building an Email List is so Important Today (6 Reasons)

    Revealed: Why Building an Email List is so Important Today (6 Reasons)

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. Astha says:
    Mar 29, 2017 at 7:16 am

    Hey,
    What is the meaning of “Permissions for ads access” in your new 1.1.1 update in this plugin?
    Please explaim.

    Thank you.

    Reply
  2. reles says:
    Mar 27, 2017 at 11:07 am

    ty

    if I want two banners?

    Paragraph 2 and paragraph 5?

    Reply
    • phil says:
      Oct 11, 2017 at 1:36 am

      Just copy the function and rename one for have a different function, then choose another paragraph … easy ;-)

      Reply
  3. Fika Thiana says:
    Mar 10, 2017 at 11:17 am

    Thank you so much, I have my adsense shows in the middle of the post. I’ve tried some other tutorial but this tutorial really work like a charm.

    Reply
  4. Waleed Barakat says:
    Mar 7, 2017 at 8:13 am

    Wow! Amazing solution! working like a charm. Thank you

    Reply
  5. Micheal says:
    Feb 27, 2017 at 12:08 am

    Please which plugin can i use to add “Read Also(another post)” within my post content to link to other post within my website for users to read another post.

    Reply
    • WPBeginner Support says:
      Feb 27, 2017 at 12:25 am

      Hi Michael,

      You don’t need a plugin for that. You can link to the posts in post editor. You can also use a related posts plugin which will show similar articles from your website at the end of each post.

      Reply
  6. Ed says:
    Feb 11, 2017 at 6:09 pm

    Working. Just removed <?php
    and paste at the bottom.

    Reply
  7. Abhijit Badgujar says:
    Jan 17, 2017 at 9:44 am

    Hi,

    I installed your plugin and did every step you instructed. But I can’t see the ads in my post.

    Reply
    • Abhijit Badgujar says:
      Jan 17, 2017 at 9:50 am

      Hi, Ij changed the setting and it worked. Thanks for the plugin.

      Reply
  8. Taylor says:
    Jan 10, 2017 at 12:13 pm

    Hi. Great thanks for the code. However, is it possible to increase the space a little before and after the ad, like increasing the margin slightly or something like that. Thanks
    Taylor

    Reply
  9. Aamir says:
    Dec 11, 2016 at 2:00 pm

    what should i do if i want to put ads on videos…
    i have movie site and i want to put ads on movies videos..
    please help….

    Reply
  10. Vitor Hugo says:
    Dec 5, 2016 at 10:29 am

    Nice tip, I was looking for it… but for some reason, the code (the plugin I don’t know, didn’t use it, only the code) insert a random orphan closure paragraph tag () in the end of the post.

    Reply
  11. Sam says:
    Nov 18, 2016 at 4:18 am

    When using manual code via functions.php, how do you get the text to wrap around the ad code?

    Reply
  12. sandeep kumar says:
    Nov 5, 2016 at 12:30 pm

    I have tried you plugin INSERT ADS and completely satisfied as it doesn’t require any coding skills

    Reply
  13. Bejek says:
    Nov 4, 2016 at 3:18 pm

    how add random post in the_content after 2 paragraf

    Reply
  14. Deise de Oliveira says:
    Oct 5, 2016 at 10:52 am

    Thanks! Tha’s exactly what I was looking for!

    Reply
  15. Robert says:
    Sep 22, 2016 at 11:33 pm

    How do you place the ads in the center?

    Reply
    • Sam says:
      Nov 18, 2016 at 4:20 am

      add before ad code and end with

      Reply
  16. James Burnette says:
    Sep 15, 2016 at 10:20 pm

    I have a post adverts google ad above the content and it causes the adsense code to be shown when shared on fb. Any suggestions?

    Reply
  17. Byron says:
    Sep 12, 2016 at 3:14 pm

    Hi,

    I want to have a [show more] after the second paragraph and a [/show more] after the last paragraph of all posts, to show only on mobile devices.

    I’ve tried ad inserter but the close [/show more] doesnt quite work. Is there anything you could recommend for me to get this working?

    Thanks

    Reply
  18. Syed Irfan says:
    Aug 9, 2016 at 11:36 am

    What to do if i want to add more Ads?

    Reply
  19. Arta says:
    Jul 18, 2016 at 7:25 am

    How can I have a Facebook box slide up at the end of the content?
    Just like list25.com

    When you finish reading and you reach the end of the content, facebook box slides up

    Reply
    • WPBeginner Support says:
      Jul 19, 2016 at 4:34 pm

      List25 uses custom code for that. May be we will add a tutorial about it in the future. Meanwhile, did you check OptinMonster‘s slide in optin? It is a lot easier.

      Reply
  20. Arta says:
    Jul 18, 2016 at 7:05 am

    Thank you

    it worked well.

    Reply
  21. Umer Shah says:
    Jul 10, 2016 at 4:07 pm

    Hi,

    I am using a insert post ad plugin on my blog, its working fine except the position of the ad which is left. How can I move it to center?

    Reply
  22. Alex Sume says:
    Jul 9, 2016 at 8:08 pm

    Hello,
    I use this code and it works well on my blog. Now i want to add more 1 extra ads code to display after another paragraph.
    Please help.

    Reply
  23. Vincent says:
    Jul 1, 2016 at 11:41 am

    The plugin method is great and I’ve added a couple of ads; one appears after 3rd paragraph and the other appears at the end of the content. This is exactly what I’m looking for since editing the code is always a pain for me every time my site’s theme gets updated. I know I should be using a child theme, but that’s just a lot of mess — your method is much cleaner and less headache. Thanks.

    Reply
    • WPBeginner Support says:
      Jul 3, 2016 at 11:40 pm

      Glad you found it helpful :)

      Reply
    • Salman Naqvi says:
      Aug 2, 2016 at 1:56 am

      Hey , I also Implemented this code and it worked well ! Now i want to add code to show add at the end of the content ? How Would I Change this php code to show add at the end ?
      Regards

      Reply
  24. Josh says:
    Jun 13, 2016 at 2:09 pm

    Is there a way to set which ads will show for various post categories? For instance, I might show a different CTA for a technology post than I would for finance post.

    Possible?

    Reply
  25. Muhammad muneeb says:
    May 19, 2016 at 7:04 am

    I want to add ads in the side of post content please help me for this.

    Reply
  26. shane says:
    May 16, 2016 at 10:01 am

    Not happy now. Added the code and now my site is broken. What have I done wrong. I cant even load my site anymore?

    I added the code to the bottom line as is explained in here. then removed the “ads code goes here” and replaced with my adsense code. But now my site wont even load. HELP please.

    PLEASE

    Reply
  27. Arun Kallarackal says:
    May 10, 2016 at 4:46 pm

    Hi Syed,

    The site specific plugin method you provided is working like a charm for me. Thanks!

    Is there any way to trigger the site specific plugin only if a blog post has a minimum number of paragraphs?

    For example- I want the ad to appear only if the blog post is at least 10 paragraphs long. Is there any way to add that criteria to the site specific code you provided? The ad should appear only if the minimum para criteria has been satisfied.

    Any help would be appreciated!

    Arun

    Reply
  28. pawan says:
    May 6, 2016 at 7:24 am

    Not working at all.. I help try all the steps but it doesn’t work. Any help.!!

    Reply
  29. Antonia says:
    Apr 4, 2016 at 3:29 pm

    My functions.php file is full of stuff. Where exactly do I paste the code? Thank you!

    Reply
    • WPBeginner Support says:
      Apr 4, 2016 at 7:15 pm

      At the end of the file. If you have a ?> tag at the end, then paste the code before it.

      Reply
  30. Srijan Singh says:
    Mar 6, 2016 at 10:47 am

    Hey, I added above codes to my functions.php file but then I got these errors:

    Warning: session_start(): Cannot send session cache limiter – headers already sent (output started at /home1/srijan1998/public_html/football2mania.com/wp-content/themes/gameday/functions.php:1320) in /home1/srijan1998/public_html/football2mania.com/wp-content/plugins/accesspress-social-counter/accesspress-social-counter.php on line 139

    Warning: Cannot modify header information – headers already sent by (output started at /home1/srijan1998/public_html/football2mania.com/wp-content/themes/gameday/functions.php:1320) in /home1/srijan1998/public_html/football2mania.com/wp-includes/pluggable.php on line 1228

    so I decided to logout my dashboard, i tried to login back by going to my w-admin page, these are errors showed up on blank screen.

    I’m just not able to login, could you help please?

    Reply
    • WPBeginner Support says:
      Mar 6, 2016 at 11:45 pm

      Please see our guide on what to do when you are locked out of WordPress admin area. Remove the code from the functions file.

      Reply
  31. Solomon says:
    Mar 5, 2016 at 8:59 am

    Please where do I implement this code in the functions.php file. You didn’t mention to insert the code after a specific line. Thanks

    Reply
  32. Donovan says:
    Feb 25, 2016 at 10:22 am

    Thanks for the plugin. However, when I deselect the posts and pages options in the settings section it will not save that setting.

    Either posts or pages or both have to be checked for the settings to be saved.

    Is there a way around this?

    Reply
  33. Denis says:
    Feb 21, 2016 at 5:59 pm

    Hi,

    Plugin works fine but how can i center the div class?

    display: block margin left right auto; text-align: center….nothing works :/

    Thx

    Reply
  34. Raymond says:
    Feb 13, 2016 at 9:58 am

    Cool! Thanks for the great tip. I manage to add my banner in every page and post.

    Cheers!

    Reply
  35. Husnain Naz says:
    Feb 7, 2016 at 9:31 am

    Can you please tell me how can i display the ad before 1st paragraph. I am using div float right and would like to display the ad beside first paragraph

    Reply
  36. Indy says:
    Feb 1, 2016 at 4:30 pm

    Hi,

    Great code snippet. How can you use it to add more than one advert? I need to add two adverts inside the post. This works great for the first one after paragraph 10 but when I tried adding:

    if ( is_single() && ! is_admin() ) {
    return prefix_insert_after_paragraph( $ad_code2, 16, $content );
    }

    where $ad_code2 is the second adsense code, it shows nothing?

    Any help on this will be highly appreciated :)

    Thanks
    Indy

    Reply
    • toxicchili says:
      Feb 17, 2016 at 5:57 am

      Don’t return the content before you have added both ads.

      if ( is_single() && ! is_admin() ) {
      $content = prefix_insert_after_paragraph( $ad_code1, 10, $content );
      $content = prefix_insert_after_paragraph( $ad_code2, 16, $content );
      return $content;
      }

      I implemented a loop add the code in for each adverts before returning the content.

      In my example I am actually storing my adverts in a database then based on the targeting criteria I return an object with all the adverts for the specific ad unit placement. It them loops and add each advert. I have used various different classes in my plugin, so you will need to change the function name to suit yours.

      if ( is_single() && ! is_admin() ) {
      $adverts = $this->get_ads_query(3);
      foreach($adverts as $advert){
      $content = $this->insert_into_content( stripslashes_deep($advert->code), 2, $content );
      }
      return $content;
      }

      Reply
    • Anubhav says:
      Feb 22, 2016 at 1:50 am

      Hi Indy

      You can use the other filter

      Given code this

      add_filter( ‘the_content’, ‘prefix_insert_post_adsNew’ );

      function prefix_insert_post_adsNew( $content ) {

      $ad_code = ‘Ads code goes here’;

      if ( is_single() && ! is_admin() ) {
      return prefix_insert_after_paragraph( $ad_code, 10, $content );
      }

      return $content;
      }

      This should work . Do not remove the previous filter just add this below the existing filter

      Reply
      • Markart says:
        Sep 12, 2016 at 3:11 pm

        You are great! thanks for second ads filter. but some posts have not enough paragraph count.

        It does not have the total count of paragraphs. I want to add to the end of the post in the second filter.

        How do I do it? sorry for my english thanks!

        Reply
  37. Kabo says:
    Jan 12, 2016 at 2:13 am

    I’m afraid the code isn’t working anymore.. any tips?
    Does it need to be under Single.PHP for some themes maybe?

    Reply
  38. Evelina says:
    Jan 9, 2016 at 11:36 am

    The plugin does not work on my blog

    Reply
  39. Mr B says:
    Dec 21, 2015 at 4:59 am

    Thank you for this code. Wow, it did exactly what I needed.

    Reply
  40. elena says:
    Nov 10, 2015 at 4:04 pm

    Hi I just downloaded the plugin and it is exactly what I needed.

    However I have a question which I hope you can answer.

    I have a lot of articles where my ads don’t apply to. Is there a way to just add the ad when I need it?

    I know you have the option to include o or exclude from the current and future posts but what about the old posts? Do I have to do this manually?

    Reply
  41. arpan says:
    Oct 28, 2015 at 1:36 pm

    Hey , will the code method conflict with the auto social media post?

    Reply
  42. Raquel Smith says:
    Oct 20, 2015 at 10:17 am

    Hello! I am looking to add a custom Pin It button after every first image in my posts. It seems like this method might work. How do I have it show up after the first image, not the first tag? Thanks!

    Reply
  43. habeeb says:
    Oct 6, 2015 at 11:01 pm

    wow! I loveth this. Thanks for the brainwave. But can I install the plugin to add ads, and at the same time I used the code method as well?

    Urgent reply Pls…

    Reply
    • WPBeginner Support says:
      Oct 7, 2015 at 5:12 am

      Yes you can do that.

      Reply
    • Ivan says:
      Oct 27, 2015 at 7:57 am

      Yes you can do that but why? Keep in mind that your site safety is comprimitted by using plugins for every little thing you want. This code works and there is no need for extra plugin.

      Reply
  44. Joseph says:
    Sep 7, 2015 at 10:46 am

    Can you please support for floating left or right ads in single post?

    Reply
    • Wayne Elliott says:
      Dec 4, 2015 at 2:09 am

      Plus one. Great plugin. The ability to right align and wrap content around next paragraph (float: right?) would be great. I fiddled around with WP custom css for #boxmap but could not get it right. Any tips.

      Reply
  45. malik says:
    Sep 5, 2015 at 11:32 am

    How can i make the ads align to the center of the post.

    Reply
    • Mohammed says:
      Dec 31, 2015 at 11:10 pm

      You can do either style the div element or add Your Ad code here tag to align the ads in center of the post.

      Hope it helps.

      Reply
  46. george says:
    Sep 2, 2015 at 12:18 am

    I’ve installed the plugin and choose the setting ‘post’ but the Addoesn’t show anywhere within the post. I’ve also added the code above to the functions.php file but it didn’t work either and was just a blank page. Can you please help

    Reply
  47. mahesh says:
    Aug 19, 2015 at 5:36 am

    when i put the code in functions.php its comming database error

    Reply
    • Xiao says:
      Dec 29, 2015 at 5:25 pm

      You have to remove the <?php tag and paste the rest of the code in functions.php file.

      Reply
  48. Nate says:
    Aug 18, 2015 at 3:08 pm

    How would this function work if you wanted to add two ads. Say one after the 2nd paragraph and then another one after the 4th paragraph?

    Reply
    • Michael says:
      Oct 6, 2015 at 5:49 am

      Yes it would be good to know how to do this? I would also like to add in another advert in paragraph 4 for example but duplicating the code results in an error

      Reply
  49. Favour Ikandu says:
    Jul 21, 2015 at 3:19 pm

    Please I’m new to blogging and ads, not to talk of the codes. I want to know if its possible to to add the AdSense code in posts using a smartphone.

    Reply
  50. Nabeel says:
    Jun 26, 2015 at 1:59 pm

    Hi, wanted to know that why do you have above the fold ad on your site?

    Please elaborate, as I read it’s bad for seo?

    Reply
    • WPBeginner Support says:
      Jun 26, 2015 at 11:02 pm

      We do not have any ads above the fold. We have a banner which expands and shows links to different sections of our website. We also don’t think that above the fold ads are bad for SEO unless they are disruptive. Particularly on mobile devices, ads placed just before the content make it difficult for users to tap on links.

      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
TrustPulse
TrustPulse
Instantly get 15% more conversions with social proof. 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)
A2 Hosting Coupon Code
A2 Hosting Coupon
Get 66% off A2 Hosting 's premium WordPress hosting.
Liquid Web
Liquid Web Coupon
Get 50% OFF on Liquid Web managed WordPress hosting plans for 3 months. From just $9.50.
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.