Here is a story that prompted me into writing this short tutorial on how to insert any Ad network within your wordpress post content.
I actually decided to tweak my blog a little bit this morning, and what came to my mind was placing an additional unit of Google Adsense Text Ads into my post content. I choose to add the code within my post content after the second paragraph of all my articles, but it wouldn’t have been possible editing about 500 articles just to add the Adsense code into the second paragraph each. I had to make a little research and came up with some snippets that helped me achieve the task.
The most amazing part of my story is after placing the Adsense unit within the post, I had a better CTR and it increased my earnings today.
- Must Read: How Many Adsense Units Per Page are Allowed
- How To Insert Adsense Ads Below Post Title In Blogger
Enough of my story, let me give you the code to add any sort of Advert within your post content after the second paragraph. It would place the Ad automatically to all your blog post.
Adding Adsense After Second Paragraph In WordPress Posts
1. Login to your WordPress Dashboard
2. Click on Appearance > Editor on the left side bar.
3. Now click on functions.php on the right side bar of the page that loads, it is the function file of your current theme on your blog.
4. In the very last line of the edit box that just loaded, paste the below code
//Insert ads after second paragraph of single post content by oscarmini.com.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
$ad_code = '<div align="left">Your Ads Code 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 );
}
5. Then Save.
Remember to replace the place written Your Ads Code Here in the code above to your Adsense or other Ad network code.
- Must Read: How To Increase Your Adsense Earning
You would be glad you did as this has simply increased my Adsense earnings. I hope you share this article and leave your useful comments.
14 Comments
Hi Oscar,
This is a great tutorial. We can use a WordPress plugin also like Wp-Insert to insert any Ad network within your WordPress post content.
Thanks for the contribution Raplus, never knew of the plugin. Though I love doing lots of stuff without plugin in order to improve my blog’s load time. All the same, thanks for your valuable contribution.
It’s interesting. This is such a nice tutorial. Thank you for sharing this awesome post.
I’m glad you love it and Thanks for dropping by.
nice post dude… i am now making use of this on my weblog … its kinda amazing ……. the problem am having now is how can i insert another one in the 3rd paragraph???
Nice one Oscar!
I really appreciated this wonderful tip and this has stopped the nightmare of thinking about how to reduce the number of Plugins on my site
Thanks bro!
I’m glad I could help. Thanks for letting us know it did work for you
Thanks for sharing..! that is a useful to do that but I have less knowledge about coding, so this is a bit difficult for me. Anyway, thank again
You are welcome pal and thanks for leaving your valuable comment. It’s been quite a while I saw you down here.
Bro I’m using carrignton .mobile theme
But I can’t locate the functions.php on my editor side
So where do I locate it?
Tnx a lot for ur interesting posts
It should be there. Otherwise try using file Manager via cPanel.
How to display number of page views on a post?
– WordPress …
Can you tell me pls ?????
Wow!!! It really worked for my website doronize.com
Thanks so much bruh