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.






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
Why are there brackets around your add_filter code?
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?
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.
Can you do tag based customization in that? I checked their demo, saw only the option for categories.
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.
You would have to filter the_content() to make this work. In short, write this as a function, and then do something like:
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.
how to place ads on paragraf 3 and paragraf 5 without plugin ? can u help me?
You would create an elseif statement that will say i == 5 then do this.
very useful snippet
Can you use this code to add more than one instance of Ads? Say top and bottom of article?
This is to insert it WITH IN the content, so after the first paragraph, and the 10th paragraph… sure you can do that. The other article about signature that we posted: http://www.wpbeginner.com/wp-tutorials/how-add-signature-ads-post-content-wordpress/
(With few tweaks) Can be used to add ads at the top and bottom of the article.
Where do the ads come from?
They are your ads. You can have an account with numerous advertising companies such as Google Adsense etc.
Found! Been searching this code. Thanks for the simple yet detail explanation. Implemented in my blog
Thanks for this post, it saved a lot of time on the adding adsense code on each and every post on the site.
This was an incredible post. You’ve saved us all from lackluster CTR! A++
hi, this code is ignoring [/caption] therefore jumping inside caption which apparently qualifies as first paragraph. Any ideas?
@leesniak You can add an elseif statement.
Hi, can help to explain more about this. Trying to figure out myself but end up nothing works.
But what when I need paste 2 ads? One after one paragraph and second after third paragraph.
But what when I need paste 2 ads? One after one paragraph and second after third paragraph.
How could this be modified to be used on the front page with posts?
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
Had to fix line 5 … i++
for ($i = 0; $i <count($content); $i++) {
Thanks!
@Joe
Thanks for the loop fix, I was trying to figure it out myself.
Hi,
Could someone please tell me how to do this for a page?
The exact same way. Just open your page.php file and paste the codes.
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.
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.
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