How to Insert Adsense ads Automatically 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

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

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

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

27 Comments

 Add your comment
  1. EXCELLENT POST!!!!!!!!!!!!!!!!! *BRAVO*

    Vijesh, I love this post. Thank you very much. You are AWESOME!!!! You are a GREAT teacher who knows how to give instruction step-by-step, as CRYSTAL clear. Hurray!!!

    One more thing on that “Ad Injection” I’ve read their website a little bit and they said that if a user is using “WP Minify” — turn OFF the HTML, or uncheck it.

    Also, if someone is using the “WP Super Cache” — turn the cache off, otherwise it doesn’t work. Their website says that, by the way, turn OFF the cache. I guess I’m okay by this.

    Only thing about NOT turning on the cache is, your site is not getting cached. But you can always DELETE the cache even if it’s turned off.

    Anyway, keep COOL on this summer day. Have a great week!!

    Angela

    • P.S. Vijesh, I guess I don’t really need the WP Super Cache to be turned ON because I have the WP Minify installed anyway and Rob said this helps the blog’s speed goes faster.

  2. Great plugin man, great tutorial as well. Best regards from I. C. Daniel

  3. Siddhartha Sinha

    Hello Vijesh,

    I think by using this Plugin the problem inserting Adsense will be resolved easily and in a nice manner.
    Second you explained it in a fabulous way and I appreciate your tutorial with screenshot.
    This will help us in guiding the installation of plugin

    Regards
    Siddhartha

  4. adesanmi adedotun

    Hello Vijesh,

    You are not a teacher but a mentor who really love taking one by hand when it cimes to learning and making things very easy to do at one disposal… Thanks for this!

  5. Ad Injection is what I have been using for a long time and is one of the first plugins I install when I start up a new blog. I used to use Adman, but had to move to another one when the creator stopped maintaining it and it stopped working as well with newer versions of WordPress. I don’t have this problem with Ad Injection though. 🙂

  6. Theodore Nwangene

    A very interesting plugin Vijesh,
    I’ve heard about many other such plugins before like Easy Adsense, Quick Adsense, Post page and a lot more.

    I’ve also used few of them which i found really useful. But, I’m recently having an issue with my adsense account. I’ve tried so many times to insert ads on my posts but, each time i configured that, the ad will not be showing.

    And, when i now visit my adsense account, i will find out that the new adsense codes i created has been deleted leaving only the ones i created long ago.

    I don’t really know why this is happening nor how to fix it. Thanks a lot man and, have a great day 🙂

  7. I need to ask you a question. This plugin can work on any WordPress version or latest versions only?

  8. Hi Vijesh,

    Thanks for sharing such a huge list of great plugins. Your post always helps the bloggers in different ways.

  9. Extremely awesome plugin i will certainly try this one by the way i always find something great on your blog…always nice

  10. Nice post vijesh.
    You have given the alternative way to place ads inside the post. I used to pasted the code in between Single.php and 404.php
    Your method seems cool
    Keep up the good work. Missed u on my blog 😛
    Have a great weekend 🙂 🙂

    • Hi Ashi,

      I never thought of putting the add on 404.php. Great idea!

      Thanks,
      Angela

      • Well Sometimes people just lost on website so it could be great to display ads on 404 .
        Unless it does not violate any TOS of publisher program
        🙂 Have a great weekend

        • Ashi, I haven’t done that. I’m not sure I know how to insert an ad on 404. I know that there is this one page on my blog that when you click “photos” on that slider it goes to that description and on the bottom my ads appears. Now, I didn’t do that. It just went automatic.

          Angela

  11. great tips i was using ad insert-er but know gonna use this one. and really that 404 page adds idea is great .

  12. Thanks for sharing the plugin but i don’t wanna switch to other plugin at the moment as i don’t need one although plugin i am using currently doesn’t have lots of features like this one.
    But you have installed lots of plugins.

  13. Hey Vijesh, great information on the plugin to insert ads automatically. This would be a great help to all the bloggers. thanks dude for sharing with us.

  14. Nice post Vijesh,

    I am also a part time blogger and running three websites. I am using “Prost Layout Pro”which is also a alternative of the same. It is not automatic injection inside the post, you need to add more tag to place ads individualy in all post. so its bit of more work, following the same, the p[lugin you suggest is better.
    I will check at my site and rever back on the same.
    Thanks Vijesh

  15. Great plugin bro ! I was doubtful about adding ad sense on my word press blog.Thanks for the information! It got fixed:)

  16. I have used this before also , this plugin stop working when your blog gets heavy traffic and page load time is slow then plugin stop responding.

  17. Thanks for the detailed tutorial @Vijesh. I prefer ad injection for its ease!

  18. good job…i want to put ads on start of post with wrapping text on right side like you automatically. i don’t want to install any plugin i want to do this manual via code or editing wp files???

  19. sir, can we block any person from a particular place if he/she is doing more clicks on our adsense ads using Ad injection ? thanks

  20. how about posting the adsense before the first paragraph? how can I do that?

  21. Indeed a worthwhile post. Thanks for the information.

Leave a Comment

Your email address will not be published.

CommentLuv badge

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!