Here is a story that prompted me into writing this short tutorial on how to insert any Ad network within your wordpress post content.

how to place ads within content in wordpress after first or second paragraph

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.

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.

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

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

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

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here