Beginner's Guide for WordPress / Start your WordPress Blog in minutes

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:

//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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

208 CommentsLeave a Reply

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

    Thank you.

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

  2. 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.

  3. 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.

  4. Hi,

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

  5. 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

  6. 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….

  7. 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.

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

  9. 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?

  10. 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?


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

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

  12. 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?

  13. 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.

  14. 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.

  15. 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.


  16. 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.


  17. 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!


  18. 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/ in /home1/srijan1998/public_html/ on line 139

    Warning: Cannot modify header information – headers already sent by (output started at /home1/srijan1998/public_html/ in /home1/srijan1998/public_html/ 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?

  19. 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

  20. 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?

  21. Hi,

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

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


  22. 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

  23. 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 :)


    • 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;

    • 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

      • 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!

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

  25. 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?

  26. 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!

  27. 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…

    • 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.

    • 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.

  28. 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

  29. 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?

    • 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

  30. 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.

  31. 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?

    • 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.


Leave A 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.