How to Insert Adsense ads in all Posts in a WordPress Blog

When it comes to monetizing a WordPress blog with ads under a post title or below the post or in between the posts it is so simple to do with a plugin. If you have a blogger blog then you need to open the template through blogger dashboard and can embed the ad code. But in WordPress if you want to do it manually then you need to spend some time with the functions.php file in your theme page or else you need to manually place code in each and every post. If you don’t want to mess up things with a php code or waste your valuable time by pasting the ad code in each and every post, then to ease your efforts there is a simple plugin I would recommend which is called as Ad injection and don’t worry this is a free plugin.

Installing Ad injection:

All you need to do is search for Ad injection in the search plugins page and install this plugin and activate it.
ad injection How to Insert Adsense ads Automatically in all Posts in a WordPress Blog

Benefits of Ad Injection:

  • This plugin is not limited to only adsense or few publishers you can add ad code from any publisher and even you can add your own custom banner ad (e.g. Google AdSense, Amazon Associates, ClickBank, TradeDoubler, etc).
  • Have more control of random ads by exactly determining the number and placement.
  • Can disable ads on few selected pages like Home, Archives, search, about us, contact us etc.
  • Can handle CSS codes easily to float the ads to either left right or center.

Configuring Ads through Ad injection:

After installation you can get this plugin in the settings page and when you click on it you will be directed to the ad injection settings. Here you have a variety of option to play with. First you would be having a widget where you have the ability to enable and disable ads in whatever page you choose.
ad injection 2 How to Insert Adsense ads Automatically in all Posts in a WordPress Blog
In order to enable your Ads you need to click on the On: Ads enabled
If you want to disable Ads you can select Off this is done when you are effected with click bombing. You need not remove your ad code but simply you can check off and your Ads won’t be displayed.
Third option is Test Mode where ads can be visible only to admin so that he can test and see how ads look after enabling them to display. After you feel the ads placements are good you can enable them to all visitors.
ad injection 3 How to Insert Adsense ads Automatically in all Posts in a WordPress Blog
Immediately after these three options you have an option called Tick to disable ads. Here you can disable ads on pages like 404, home page, search page, Archieve pages etc. So if you don’t want your ads to be displayed on your about us, Contact us, Privacy policy you can disable them by checking the appropriate box. You can also disable only a particular ad unit too it could be a top ad, bottom ad or even a random ad for a particular page.
Next you would have some more options where you can select options like displaying ads only on posts which are longer than 100 words, 200 words or as per your wish.
ad injection 4 How to Insert Adsense ads Automatically in all Posts in a WordPress Blog
You also have an option to set Maximum number of random ads per post. Adsense and even few publishers support only 3 ad units or some four so you can limit the random ads to 2 or 3 depending on the Terms of your publisher. There are more options in this section which can be easily understood.
Immediately after the settings widget you have the important widget where your adsense code or any other ad code can be place. You can also adjust the placement by few options given in the right side. You can float your ad left, right or center. You can increase the margins or padding that all depends on how you want your ad to look in the space.
So there are four sections like Top ad it displays ads immediately after the post title, Random ad which is displayed randomly in between posts, bottom ad which displays ads below the post content and footer ad which displays ads on your footer section. Below these there are more options for an advanced user but for a basic user the settings which I have discussed are more than enough.

How to place ads without a Plugin:

If you want to place ads without using a plugin then you need to go with this method where you need to open your functions.php file of your theme 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>Ad 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 );
}
In the above code you need to place your code where it says ad code goes here. If you want your ad code to be displayed after third paragraph you need to change the number 2 which is in red to number 3. Personally I use Ad injection plugin to configure ads in my WordPress blog. There are also many plugins to insert ads like Wp-Insert which also has almost same functions like ad injection. Now its up to you how you monetize your blog.

Credits for Code: Wpbeginner

Leave a Reply

Your email address will not be published. Required fields are marked *