A Simple Ad Rotator for WordPress

Ads in [tag]WordPress[/tag] headers, footers, and sidebars are repeated over and over as visitors move around a blog. It is preferable to have the ad(s) change every time the template is loaded. Changing ad layouts reduce ad blindness while changing ad providers gives visitors more choices. However, rotating ad scripts in WordPress is not so easy.

Google allows you rotate up to (4) different palettes for the same AdSense ad. OK – better than nothing. But, what if you want to rotate different AdSense ad formats (text/image/size) or mix in other ads such as Amazon ads?

The Adrotator Plugin supposedly addresses this need. This plugin, however, requires each ad to be placed in a text file in a single line. If the ad code is multi-lined, they recommend: either converting the ad code to single line manually or just take the most relevant portion of the code at the end and insert it in the file. The remaining code (which normally will be used to setup some parameters) will remain above the location (in your template file) where you would insert the function to select an ad from this ad group. Yikes! They lost me there. Is there another way to do it?

I have had very good results using a simple PHP script such as:

<code><?php
$fcontents = join ('', file ('./ads/rotate1.txt'));
$s_con = split("~",$fcontents);
$banner_no = rand(0,(count($s_con)-1));
echo $s_con[$banner_no];
?></code>

Place your ads separated by a ‘tilde‘ in a text file named rotate1.txt. The text file should look like this:

Ad code 1
~
Ad code 2
~
Ad code 3

Then, upload the text file to a folder named in the root directory. Be careful specifying the path to the text file. In this example, it is in a folder named ‘ads’ in the root directory. If you have any problems, try putting in the full path such as: ‘http://www.mysite/ads/rotate1.txt’

Next, simply paste the PHP script into the template file where you normally would paste the ad code.

You can center/align the ad with a

tag such as:

or

using your stylesheet.

You are done!

OK. So this is not a plugin! Do you really need a plugin to do something this simple? Once you paste the code into your template file(s), you can forget about it. Adding/deleting ads is snap. Just edit the text file, then upload it to your server.

One Comment