Insert Adsense Ads Programatically Anywhere Inside Post

If you run a blog website, most probably you will be inserting ads in your blog to generate some income. Content ads get the best click through rate. If you want to increase your CTR and Google AdSense revenue, then you need to start adding ads within your post content.

Using a plugin is really simple, isn’t it? You must have come across several WordPress plugins for integrating Adsense into your WordPress powered blogs be it in the sidebar, between posts, in between the content and other areas like footers and headers. There are various ways by which we can add AdSense ads in between the post. That including editing your theme file or using any WordPress plugin which randomly add AdSense ads in between the post and around the post. Adding AdSense at a random position has the advantage to avoid ad blindness but this process usually break your post and make it look ugly. I have always been looking for some easy and quick solution which will let me add AdSense in between the post automatically.

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. I tried several plugins including Insert Post Ads, AdSense Integration WP QUADS etc, which are good but does not give me exactly what I was really looking for.

The reasons why I dislike using those plugins is that I have to stuff shortcode inside post content again or the ads doesn’t appear exactly where I wanted them to appear nor could I style it the way I want. It is a bit tricky when it comes to placing the Adsense code in between the article body. That is exactly what we are going to focus today. After much effort, I came up with this method that is without using any plugin (see this post as a demo) and I’m sure you will find it worth putting and will take advantage of this useful tips to maximize your AdSense earning. In this article, I will show you how to insert three or more ads within your post content after any paragraphs of your choice automatically and style it the way you want. I will look at ways using which you can manually place the Adsense banner [or any other AD serving agency] by implementing some really simple code snippets. I will provide you the code, don’t worry!

Open your theme’s functions.php and paste the following three codes:

The first code will enable ads right after your first paragraph. The second code will enable ads right after your third paragraph. And the third code will enable ads right after your fifth paragraph. All the three ads will float left automatically. The magic in this code is”$content_block[1]”. Change the number block[1] to see the changes. block[0] means before the first paragraph, block[1] means after first paragraph block[2] means after second paragraph and so on. This default method perfectly fit in a posts that contain five or more paragraphs. Do let me know if you have other awesome ways to insert Ads inside post automatically.


add_filter('the_content', 'wps_add_incontent_ad1');
function wps_add_incontent_ad1($content)
{ if(is_singular('post')){
$content_block = explode('<p>',$content);
if(!empty($content_block[1]))
{ $content_block[1] .= '<div style="float:left; max-width:300px; max-height:250; margin-right: 10px;">Ads code goes here</div>';
}
for($i=1;$i<count($content_block);$i++)
{ $content_block[$i] = '<p>'.$content_block[$i];
}
$content = implode('',$content_block);
}
return $content;
}


add_filter('the_content', 'wps_add_incontent_ad2');
function wps_add_incontent_ad2($content)
{ if(is_singular('post')){
$content_block = explode('<p>',$content);
if(!empty($content_block[3]))
{ $content_block[1] .= '<div style="float:left; max-width:300px; max-height:250; margin-right: 10px;">Ads code goes here</div>';
}
for($i=1;$i<count($content_block);$i++)
{ $content_block[$i] = '<p>'.$content_block[$i];
}
$content = implode('',$content_block);
}
return $content;
}


add_filter('the_content', 'wps_add_incontent_ad3');
function wps_add_incontent_ad3($content)
{ if(is_singular('post')){
$content_block = explode('<p>',$content);
if(!empty($content_block[5]))
{ $content_block[1] .= '<div style="float:left; max-width:300px; max-height:250; margin-right: 10px;">Ads code goes here</div>';
}
for($i=1;$i<count($content_block);$i++)
{ $content_block[$i] = '<p>'.$content_block[$i];
}
$content = implode('',$content_block);
}
return $content;
}

Leave a Reply