WordPress.org

Ready to get started?Download WordPress

Plugin Directory

Ad Injection

Injects any adverts (e.g. AdSense) into the WordPress posts or widget area. Restrict who sees ads by post length/age/referrer or IP. Cache compatible.

Why was this plugin created?

I used to use the excellent Adsense Injection by Dax Herrera, but found I needed more features and flexibility. His plugin inspired this one.

How is this plugin different to Adsense Injection by Dax Herrera?

One a basic level it can do the same job as Dax's Adsense Injection. If you want it can just inject AdSense between paragraphs like his plugin does. I used to use his plugin, but found that I wanted a lot more features. Here are some of the extra features.

  • Inject any type of advert from any ad provider.
  • Restrict ad display by referrer (e.g. can restrict display to search engine visitors).
  • Can prevent specific IP addresses from seeing adverts.
  • Can define randomly positioned adverts, and adverts at the top, bottom and footer of the posts.
  • Add adverts to the widget area.
  • Adverts can be positioned anywhere in your theme template.
  • Ad rotation / split testing.
  • Restrict adverts by category, tag, author, post ID.
  • Override ad positions for individual posts if necessary.
  • Vary number of adverts based on post length.
  • You can inject raw JavaScript and PHP.
  • Include ads anywhere in the theme template if you edit your theme's PHP.
  • The dynamic features (ad rotation, restricting ads by referrer and IP) work with WP Super Cache, W3 Total Cache and WP Cache.
  • Define alternate content for users who are dynamically restricted from seeing ads.
  • Compatible with the in-page tags from Adsense Injection to make migration easy.
  • Compatible with in-page tags from Whydowork Adsense and Quick Adsense.
  • Extra positioning options - for example you can force the first advert to be right after the first paragraph so that it will be 'above the fold'.

Thanks to Dax by the way for providing the inspiration for this plugin!

Does this plugin 'take' a percentage of my ad earnings?

No! Absolutely not. Some ad plugins replace your publisher ID with their own for a certain percentage of adverts. Ad Injection does NOT do this. All your earnings are your own. Ad Injection makes no modifications to your ad code. What you paste into the ad boxes is what is injected into your pages.

Is using this plugin allowed for Google AdSense?

As far as I can tell using this plugin should be legal for AdSense as long as you make sure the ad quantities/placements comply with their TOS. However it is up to you to make sure that your website complies.

Ad Injection is designed as a generic plugin for injecting all types of adverts. It will not specifically check that the defined ad quantities or positions are in compliance of the AdSense TOS for you. For example Ad Injection will allow you to inject more ads than Google allows if you configure it to do so.

Note that AdSense has limits as to the number of adverts you can include on a page. If you try to include more then you will end up with 'blank boxes' on your page.

Be careful if you use the float left/right positioning options. These options could cause your AdSense adverts to appear under other elements of your page if you have also set a float value for them (e.g. floating images, and adverts together could be problematic). However the new 'clear' option should allow you to make sure this doesn't happen.

The best advice for any advert plugin is to manually check that you are happy with the advert quantities and positioning it produces, to ensure that you comply with the TOS for your ad program.

You use this plugin at your own risk.

Do you have any testing recommendations?

For testing your ad settings I'd recommend that you first disable any caching plugin, and then set Ad Injection to test mode so that 1) you can instantly see any changes you make to the settings and 2) so that only you will see the adverts.

If you are unsure as to why ads are appearing (or aren't) enable debug mode from the UI and look for the 'ADINJ DEBUG' tags in the HTML source of your webpage.

When you are happy with the ad quantities / positions you can disable debug mode, re-enable your caching plugin, and set the Ad Injection mode to 'On'.

If you are testing the search engine referrer settings be aware that Ad Injection sets a one hour cookie when you visit via a site with a matching referrer. This means that after you have visited the site via the matching referrer the adverts will keep showing for the next hour. Clear your cookies to reset the behaviour. The Firefox 'Cookie Monster' plugin is very useful if you want to check the status of the cookie. Look for the 'adinj' cookie. Instead of clearing all your cookies you can just delete this one.

Using a second browser in 'privacy mode' is also a good way of testing your site with a clean slate. A browser like Google Chrome will allow you to test your site with no cookies definied if you start a new private browsing session.

Do I need to have WP Super Cache (or anther caching plugin) installed?

No! All the features of this plugin will work with no caching plugin installed. But if you do have WP Super Cache the dynamic features (ad rotation and enabling ads based on IP address and referrer) will still work. And your blog will (probably) run a lot faster than with no caching plugin. Usually caching plugin prevent dynamic plugin features from working - but I've spent a lot of time creating a framework to allow this plugin's dynamic features to work with some of the most common caching plugins. Just make sure you choose the mfunc dynamic insertion mode from the Ad Injection settings screen.

Will the dynamic features work with other caching plugins?

The dynamic features will work with any caching program that supports the mfunc tag. At the moment that is WP Super Cache, W3 Total Cache, and WP Cache.

Which caching plugin is best?

Both WP Super Cache and W3 Total Cache are likely to be faster than WP Cache.

If you aren't using the dynamic features then the only way to find out which is best between WP Super Cache and W3 Total Cache is to measure the performance yourself, as the speed depends on many factors that will be unique to your web server and web site.

If you are using dynamic features with mfunc mode then WP Super Cache (in mod_rewrite mode) and W3 Total Cache (in Page Cache: Disk (basic) mode) are likely to offer similar performance as they both return the dynamic files via PHP.

Note that W3 Total Cache will not cache pages with mfunc tags if you use the Page Cache: Disk (enhanced) mode.

The speed of your website does depend on more factors than just page serve time so other features that the caching plugins offer (such as CDN and minification support) may swing the advantage either way.

WP Hyper Cache does not support mfunc tags so only use it if you don't want to use any of the dynamic features. If using WP Hyper Cache set the Ad Injection mode to 'direct'.

For reference: dynamic features are ad rotation, and blocking ad views by IP and referrer.

What if I am using an incompatible caching plugin?

Don't worry - everything will still work except for:

  1. Filtering ads based on the IP address of the visitor.
  2. Filtering the ads based on the HTTP referrer of the visitor.

If you aren't interested in these features then it doesn't matter! Just make sure you tick the box to say that you to use 'Direct static ad insertion' on the Ad Injection settings screen.

Can I just have adverts on the home page?

i.e. adverts on the home page, but not on single posts or pages.

Yes you can do this, there are two ways.

  1. In the 'Single posts and pages' setting set the number of injected ads to 0. Then in the 'Home page' settings set the number of ads to whatever you want.
  2. Alternatively use the global exclude options at the top to exclude ads from all page types except the home page.

Why aren't adverts appearing on my front / home / archives page?

Ads will only appear if your front, home or archives page if you show the full post - not if you show excerpts*.

*However if your theme leaves the HTML tags intact then you may in fact see the top ad in your excerpt. Whether you do or not depends on how your theme deals with excerpts.

How do I stop adverts from appearing on my contacts form page?

To stop ads appearing on the contact page (or any other post/page) you have many of options. Here are 4 to choose from:

  1. Add a disable_adverts custom field to the post/page containing the contact form and set the value to 1 (the number one as a single digit). If you can't see the custom fields click on 'Screen Options' at the top right on the post/page editing screen, and tick 'Custom Fields.
  2. Add the post/page id to the post/page id ad filtering box.
  3. Add (copy and paste) <!--NoAds--> into the content of the post/page with the contact form. Just edit the contact page and paste this in - it will be invisible to the reader.
  4. Add a tag to the page to mean that it shouldn't have adverts, and then add that tag to the tag filtering condition in the global settings area of the plugin configuration page.

How can I put ads on category (or other archive pages)?

The top, random, bottom and footer ads can be placed into the category pages. Category pages are a type of archive.

On the main settings page for Ad Injection just enter the number of adverts you want on these page types using the Archives column in the 'Ad placement settings' section.

These ads will only appear on archives/category pages if you are showing the full post contents on these pages. They won't work if you are showing excerpts. These restrictions don't apply to widget ads.

My adverts are overlapping with other page elements (e.g. images)

You can try defining the 'clear' display setting so that multiple floated parts of your page do not overlap.

If you always have a floated image at the top of the page you can set which paragraph the first random advert will start from. If you needed finer control over where the random adverts appear you can use the and tags in the page.

I have set the ads to float but the text/headings aren't flowing around them

Check your style sheet to see if either the text or headings have the 'clear' attribute set. This may be preventing your text / headings from flowing around the advert.

After adding the ads my sidebar has dropped down the screen. Why?

This is because you have inserted adverts that are too wide for your site's layout. The browser can no longer fit all the parts of your layout side-by-side. Try using ads that are less wide.

How do I add left/right margins to the ads

There aren't any options to do this in the UI, but you can just put the layout tags into the ad box around your advert. e.g.

<div style="margin-left:50px;margin-right:50px;">
Your advert
</div>

I have configured four Google AdSense ad units but only three are showing. Why?

Google's AdSense TOS only allow allow three ad units, and three link units per page. If you have for example tried to insert four ad units on your page then Google will (probably) disable the forth one automatically. Read Google's AdSense program policies for more info.

You may find that your right sidebar ad doesn't show if you have too many ads. Google renders the ads in the order that they are in the HTML, and your right sidebar will be at the bottom of HTML. If you need your right sidebar AdSense ad you will have to limit the number of ads on the rest of the page from the 'Ad placement settings'.

How can I rotate more than 10 adverts?

I am planning on making a change to allow an arbitrary number of adverts - this will probably be in 2012. Until then you have these 3 options:

  1. Use PHP / JavaScript in the ad boxes to handle the rotation of the extra ads.
  2. Use an ad service such as OpenX or Google Ad Manager in conjunction with Ad Injection. Ad Injection can handle the ad placement, and the ad service can manage your ad pool.
  3. Hack Ad Injection to increase the limit (but be aware that if you upgrade your changes will be overwritten).

I'd recommend 1 or 2.

How can I show different ads for different categories?

If you want to show different ads for different categories using widgets you can set up filters for the different ads from the widget UI. If you want to show different top, random, or bottom adverts on different categories you can use some very simple PHP in the ad box (this will also work for the widgets as well if you don't like the UI method).

Example 1 - one category specific ad and a general ad.

<?php if (in_category('japan')) { ?>
Japan advert
<?php } else { ?>
General advert
<?php } ?>

Example 2 - Two different ads in two different categories. Pages which aren't in either category have no ads.

<?php if (in_category('cheese')) { ?>
Cheese advert
<?php } else if (in_category('milk')) { ?>
Milk advert
<?php } ?>

See http://codex.wordpress.org/Function_Reference/in_category for more information on in_category.

You can do the same for tags using has_tag. See http://codex.wordpress.org/Function_Reference/has_tag for info.

Note - this will only work in direct insertion mode.

I want a different advert for each category. How can I do this?

  1. In your plugins directory create a sub-directory called 'ad-injection-ads'. e.g. /wordpress/wp-content/plugins/ad-injection-ads/

  2. Create a text files in this folder for each of the categories that you want an ad for. The text files should be named [category nicename].txt The 'nicename' of the category is the category name with spaces and dots converted to '-' and apostrophes removed. e.g.

Liverpool = liverpool.txt Manchester United = manchester-united.txt A.F.C Aldermaston = a-f-c-aldermaston.txt Bishop's Stortford = bishops-stortford.txt

  1. Then put this code (from the starting <?php to the closing ?>) into the ad box. It will load the text file ad matching the category name when the post is displayed.
<?php
$plugin_dir = dirname(__FILE__);
$ad_dir = dirname($plugin_dir).'/ad-injection-ads/';
if (file_exists($ad_dir)){
    global $post;
    $categories = get_the_category($post->ID);
    foreach ($categories as $cat){
        // nicename: spaces and dots are converted to '-' and apostrophes are removed
        $full_ad_path = $ad_dir.$cat->category_nicename.'.txt';
        if (file_exists($full_ad_path)){
            $ad = file_get_contents($full_ad_path);
            if ($ad === false) echo "<!--ADINJ CATCODE: could not read ad from file: $full_ad_path-->\n";
            echo $ad;
            break; // only show first category ad that matches
        } else {
            echo "<!--ADINJ CATCODE: could not find ad at: $full_ad_path-->\n";
        }
    }
} else {
    echo "<!--ADINJ CATCODE: could not find ad directory: $ad_dir-->\n";
}
?>

Some extra information: * This code will load one text file ad per post. If for example you had a post with the categories 'Liverpool' and 'Manchester United' it would load which ever ad it found first. * If will ignore categories that have no text file in the directory. If you have a post with the categories 'Liverpool' and 'Latest News' then it will always load the liverpool.txt as long as you don't create a 'latest-news.txt'. * This code will only work in 'direct' ad insertion mode. It won't work in 'mfunc' mode.

Expansion ideas: * Show a default advert if no text file exists. * Create multiple text files for each category and then randomly select one - an implementation of this is shown on advancedhtml * Use different code for top, random or bottom ads. e.g. you could have liverpool_top.txt and liverpool_random.txt

How can I show different ads for different post authors?

This is something I hope to build into the UI at some point. But in the mean time you can use PHP in the ad code boxes to do this: e.g.

<?php
$author = get_the_author();
if ($author == "john"){ ?>
This is John's Ad.
<?php } else if ($author == "paul") { ?>
This is Paul's Ad.
<?php } ?>

Note this will only work in direct insertion mode. It won't work in mfunc mode unless you also load in the WordPress database dependencies.

How can I show different ads to people in different countries?

If you install the Country Filter plugin (with the IP database) then you can use the following code in the direct ad insertion modes. This will not work in mfunc mode!

<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("uk"))) { ?>

UK advert

<?php } else { ?>

Global advert

<?php } } ?>

You can download the Country Filter plugin from http://wordpress.org/extend/plugins/country-filter/

You could also use the Geo IP functionality that CloudFlare offers (you can set it up for free if you are able to modify your DNS settings) http://support.cloudflare.com/kb/what-do-the-various-cloudflare-settings-do/how-does-cloudflare-ip-geolocation-work

If I restrict a widget to both a category and a tag it doesn't appear in the relevant category/tag archives. Why?

If you set an ad with a tag restriction of 'tag1' and a category restriction of 'cat1', then it will only appear on pages that have BOTH the tag1 and cat1 property.

A post in this category with that tag will have the ad.

But if will not show in the 'tag1' archive or 'cat1' category. This is because the 'tag1' archive is not part of the 'cat1' category. A tag archive can't be part of a category, and a category archive can't be part of a tag.

This means that if you add both a tag and a category restriction the ad won't appear in the tag or category archives.

If this restriction wasn't in place then it would mean the category and tag archives would start showing all ads that were set to appear in the category/tag - which could be loads! And there would be no way to remove selected ones from the archives.

Therefore if you are defining ads with both a category and a tag, you will need* to define a separate one for the category/tag archive.

I know this might cause extra work for you, but having this restriction allows more precise control of where the ads appear. This is a deliberate design decision, rather than some random behaviour.

Can I put adverts in other locations other than top, random, bottom, footer and widget?

Yes - you can put adverts anywhere, if you are willing to edit your theme template.

The adinj_print_ad function can be used anywhere in your theme to print the top, random, bottom or footer ad. It can also be used to load ads stored in text files in the ad-injection-data directory.

For example this will print the random ad. The ad will be subject to same global and dynamic restrictions an 'in content' random ad, but it will not be subject to conditions relating to the post length, post tags, categories, or number of ads already inserted.

<?php if (function_exists('adinj_print_ad')){ adinj_print_ad('random'); } ?>

Here are some more examples:

<?php if (function_exists('adinj_print_ad')){ adinj_print_ad('top'); } ?>
<?php if (function_exists('adinj_print_ad')){ adinj_print_ad('bottom'); } ?>
<?php if (function_exists('adinj_print_ad')){ adinj_print_ad('footer'); } ?>

Here is an example of how to load a text file ad from the ad-injection-data directory. They contents of the text file will be printed straight onto the page.

<?php if (function_exists('adinj_print_ad')){ adinj_print_ad('adsense-banner-wide.txt'); } ?>

The function_exists condition ensures that the ads will silently dissapear if the plugin is deactivated rather than generating an error.

If you are using template ads with a compatible caching plugin you will need to include adshow.php (once on the page) using mfunc tags. You will need to alter the code below to include the correct path. If you look in the 'Test ads' section of the main settings page you should be able to find a version of this code customised for your web server.

<!--mfunc include_once('/home/public_html/wordpress/wp-content/plugins/ad-injection/adshow.php') -->
<?php include_once('/home/public_html/wordpress/wp-content/plugins/ad-injection/adshow.php'); ?>
<!--/mfunc-->
<?php if (function_exists('adinj_print_ad')){ adinj_print_ad('random'); } ?>

How can I sell my ads / track my ad clicks?

These are advanced features which I have no plans for adding into the core Ad Injection. You might however be able to use Ad Injection with Google Ad Manager or OpenX Ad Server that should allow you to sell your own ads and track their clicks.

If you are using a 3rd party ad provider (e.g. AdSense) then statistics such as ad clicks will be available from the ad provider.

Are there any known plugin conflicts?

WP Minify

Problem: No ads appear when using mfunc mode.

If you use WP Minify and a caching plugin in combination with Ad Injection, you'll need to turn off the HTML minification in WP Minify. This is because HTML minification strips out the mfunc tags that Ad Injection uses. You can leave the CSS and JavaScript minification on if you already use them.

FeedWordPress

Problem: Random, top and bottom ads don't appear on syndicated posts.

By default FeedWordPress prevents the syndicated post contents from being passed to 'the_content' hook which is where the random, top and bottom ads are added. There's an easy fix:

From the FeedWordPress settings page go to 'Posts & Links' and then in the 'Formatting' section set 'Formatting filters' to 'Expose syndicated posts to formatting filters'.

Shortcodes Ultimate

Problem: Random ads don't show.

Reason: Shortcodes Ultimate disables the wpautop filter which adds the <p></p> tags to the page. It re-applies this filter at a priority of 99 which is after Ad Injection has run. Therefore when Ad Injection runs it can't find any </p> tags which it needs to position the random adverts.

Solution: Follow the below advice for the 'theme conflicts'.

Are there any known theme conflicts?

Ad Injection (when injecting random ads) works by looking for the end paragraph tags (<p></p>). Some themes override the wpautop filter and set it to run after the plugins. This means that Ad Injection can't find the end paragraph tags, and so can't inject any random ads. If this happens try changing the the_content filter priority from the Advanced tab in the Ad Injection UI. Try values of 100, and if that doesn't work 200.

Themes which I know have this issue include 'Avenue', 'TheTravelTheme', 'Exciter Magazine', 'Vectors', and 'Canvas' from Woothemes.

Will Ad Injection work with the multi-blog version of WordPress?

The multi-user version of WordPress are not supported - yet, however I have heard that some people have got it to work when using the 'direct' insertion mode. I hope to make it work properly with multi-blog versions of WordPress in the future.

Is there an easy way to copy my ad settings to a new blog?

  1. Go to phpMyAdmin, either from your web hosts control panel, or by using the very convenient 'Portable phpMyAdmin' plugin for WordPress on the blog that has your Ad Injection settings configured.
  2. Open up the [yourdb]_options table.
  3. Either find the adinj_options and widget_adinj rows or use this query to help you:
SELECT * FROM [yourdb]_options WHERE option_name LIKE '%adinj%'
  1. Then copy these two options to your new blogs by using phpMyAdmin on the new blogs. If you aren't using widgets then you can ignore the widget_adinj option, you need only the adinj_options value. You can use the 'Insert' tab on the new blog to do this.

Some technical details

  • Plugin stores all its settings in a single option (adinj_options).
  • Uninstall support is provided to delete this option if you uninstall the plugin.
  • Admin code is separated into a separate file so it is not loaded when your visitors view your pages.
  • When used with a compatible caching plugin Ad Injection loads its dynamic settings from a static PHP file, and the ads from disk so no extra MySQL database queries are required.
  • When mfunc mode is used the ads are saved as text files into the plugin folder. The plugin will therefore need write access to the plugins folder.
  • The JavaScript for setting the referrer cookie is inserted using wp_enqueue_scripts.
  • If there is anything I can do better please let me know - this is my first plugin so I still have a lot to learn!

Requires: 2.8.6 or higher
Compatible up to: 3.4.2
Last Updated: 2012-8-11
Downloads: 266,546

Ratings

4 stars
4.3 out of 5 stars

Support

0 of 2 support threads in the last two months have been resolved.

Got something to say? Need help?

Compatibility

+
=
Not enough data

2 people say it works.
0 people say it's broken.

100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1
100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,2,2 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1
100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1
100,1,1 100,1,1 100,1,1 100,1,1 100,2,2 100,1,1 100,2,2 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1
100,1,1 100,1,1 100,2,2 100,1,1 100,1,1 100,1,1 100,1,1 100,2,2 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1
100,1,1 100,1,1 100,1,1 100,1,1
100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,2,2 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,4,4 100,2,2
100,1,1 100,1,1 100,2,2
100,1,1 100,1,1 100,1,1 100,2,2 75,4,3 100,2,2
100,1,1 100,1,1 100,2,2 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1
100,2,2 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1
100,1,1 67,3,2 100,2,2 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1
100,2,2 100,9,9 100,3,3 100,1,1 100,2,2 100,3,3 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,2,2 100,2,2 100,1,1 100,3,3 50,4,2 50,2,1 100,1,1
100,1,1 100,1,1 100,1,1 100,1,1 100,1,1 100,2,2 75,4,3 100,1,1
78,9,7 100,2,2 100,1,1 100,2,2 100,1,1 100,3,3 100,1,1
100,3,3 100,1,1 100,4,4 100,1,1
100,1,1 100,4,4
100,4,4 100,4,4
100,6,6
100,7,7
100,8,8
100,3,3
50,2,1
100,1,1 0,1,0
0,1,0
0,1,0
67,3,2
100,1,1
100,1,1
100,2,2