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. will this code replace the entire existing code of the theme child theme?

    The <?php start is returning error

    • It will not replace your entire theme’s design. For pasting the code in your functions.php you could try removing that opening PHP tag to see if that helps solve the issue.


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

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


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

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


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

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


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

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


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


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

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

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


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

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

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


        • 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

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


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

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


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

    • 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


  13. Can you help me?

    How to insert ads before an ID element?

    Ex: i want to insert ads before

    Thanks you very much!

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


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

    Reply needed urgently please.

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

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

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

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

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

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

    Thanks again!

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

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


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

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

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

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

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

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

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

  31. 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’);

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

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

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

    • 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

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

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

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

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

Leave a Reply to Ahsan Ali 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.