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

How to Insert Ads within your Post Content in WordPress

Last updated on by
BlueHost - Recommended WordPress Hosting
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. It would seem extremely inefficient if one enters the code manually specially if you had to switch advertisers after one year on over 200 articles. We found a need to add ads within our post content in WordPress, so we came up with a solution. This article will show you a snippet that will let you enter ads within the post content after the second paragraph.

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. You can re-use this function to add other items for example another ad after the seventh paragraph or so on.

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


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

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Guy says:

    I tried applying this to the twenty twelve theme and got an error message.
    I added the code and the filter (add_filter( ‘the_content’, ‘insert_ad_inpost’ );
    ) to my functions.php.
    The ad code displays perfectly, but above it I get the following error:
    Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, ‘insert_ad_inpost’ was given in /home/xxxxxxxx/public_html/xxxxxxx.com/wp-includes/plugin.php on line 173

    any idea on how to fix this?

    Thanks

  2. Abhijeet Mukherjee says:

    Hi, thanks for this post. I am yet to test this and plan to do it soon. I use Genesis and I see that you’ve already given a solution to that in a comment, but what I am trying to figure out here is how to do this differently on different posts based on their tags. I run a tech blog, and I’d want to show a particular ad or a snippet for posts tagged Windows, and a different ad on let’s say posts tagged Chrome, in both cases after the first paragraph. How would you modify the code in that case? I suppose it’ll involve conditional tags in some way. Could you please tell me how?

    • Editorial Staff says:

      A better solution would be to use an ad management plugin like OIO Publisher. Insert the ad zone using the code above. Then control the ad inventory through OIO Publisher which lets you do category based conditionals.

      • Abhijeet Mukherjee says:

        Can you do tag based customization in that? I checked their demo, saw only the option for categories.

  3. Ada says:

    How would you do this for a theme framework like Genesis where you can’t find the php the content? Would you use the functions? Can’t find any answers anywhere.

    • Editorial Staff says:

      You would have to filter the_content() to make this work. In short, write this as a function, and then do something like:

      add_filter( 'the_content', 'insert_ad_inpost' ); 
  4. zoomingjapan says:

    If you put Google Adsense in your content, you’ll have script tags in your content.
    This will mess up your RSS feed. It won’t be valid anymore.
    I couldn’t use my original feed anymore as well as Feedburner.
    Bot didn’t validate anymore due to the script code in the content.
    I had to remove it again.

    I asked around, googled like crazy, but it doesn’t seem to be a very common problem.
    I wonder how other people manage to put Adsense code snippets into the content of their blog post and having a working RSS feed.

  5. Hebeg says:

    how to place ads on paragraf 3 and paragraf 5 without plugin ? can u help me?

  6. Jason Davis says:

    very useful snippet

  7. WordPress Club says:

    Can you use this code to add more than one instance of Ads? Say top and bottom of article?

  8. John Lawson says:

    Where do the ads come from?

    • Editorial Staff says:

      They are your ads. You can have an account with numerous advertising companies such as Google Adsense etc.

  9. Razul says:

    Found! Been searching this code. Thanks for the simple yet detail explanation. Implemented in my blog :)

  10. Joseph says:

    Thanks for this post, it saved a lot of time on the adding adsense code on each and every post on the site.

  11. Rod60000 says:

    This was an incredible post. You’ve saved us all from lackluster CTR! A++

  12. Lisa Mason Lee says:

    hi, this code is ignoring [/caption] therefore jumping inside caption which apparently qualifies as first paragraph. Any ideas?

  13. wpbeginner says:

    @leesniak You can add an elseif statement.

    • Barzrul says:

      Hi, can help to explain more about this. Trying to figure out myself but end up nothing works.

  14. leesniak says:

    But what when I need paste 2 ads? One after one paragraph and second after third paragraph.

  15. leesniak says:

    But what when I need paste 2 ads? One after one paragraph and second after third paragraph.

  16. Connor Crosby says:

    How could this be modified to be used on the front page with posts?

  17. Joe says:

    Hi, thank you for this … unfortunately it will not work for me … my post simply loops forever and ever (or something) … it just hangs and does not load. The page does not load, the browser ‘loading’ icon just turns and turns. I am using WP 3.0.4

  18. mike says:

    Hi,

    Could someone please tell me how to do this for a page?

  19. graphicbeacon says:

    Great idea, especially when you want the user to notice the ads more- that could also annoy the user. maybe there should be an option to hide them if you dont want it distracting the viewer?

    you missed the underscore in the first block of code.

    • Editorial Staff says:

      Thank you for the correction. Just fixed it.

      You can show ads only to google referrers only. Or use a jQuery script that has cookies installed. So ads will show for every user, but if they exit it, then they won’t see it. Until ofcourse they clear their cookies.

    • GrimCris says:

      You can try Ozh’ Who Sees Ads Plugin to determine who you want to see ads on your blog. You can exclude ads regular users, or you can show ads only for those people who came to your blog from Google

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.