Create a simple Random Ad widget for your WordPress blog

Here are the steps for creating a simple Plugin for a Random Ad Widget . This is also a good startup script for those (like me) wanting to learn how to create Plugins and Widgets for WordPress.

The idea here is to create an Ad delivery widget that will show a random ad on every visited page. Firstly I will show you the whole code, for those who know what they are doing and just want to diss my code and style. Then it will be broken into pieces with detailed explanation for those interested.  If you want to skip it all just download the source code here.

For the sake of this exercise I will call the file random_ads.php but feel free to call it whatever you like. Here we go…

This is the code for the plugin currently (Nov/Dec 2010) used on the sidebar of this site, if you refresh this page you should see a different ad appear on the sidebar.

<?php
/*
Plugin Name: Random Ads
Version: 0.1
Plugin URI:  
Description: My Random Ads, mainly Think Geek Stuff (by Alex costa)
Author URI: http://itehchlog.com
*/
?>
<?php
function widget_random_ads($args) {
extract($args);



echo $before_widget;

$random_deal[] = ‘<a href=”http://www.anrdoezrs.net/click-4163564-10769259″ target=”_top”>
<img src=”http://www.awltovhc.com/image-4163564-10769259″ width=”250″ height=”250″ alt=”16 Port USB Hub” border=”0″/></a>’;

$random_deal[] = ‘<a href=”http://www.jdoqocy.com/click-4163564-10764658″ target=”_top”>
<img src=”http://www.lduhtrp.net/image-4163564-10764658″ width=”250″ height=”250″ alt=”ThinkGeek Use Your Brain” border=”0″/></a>’;

srand(time());
$sizeof = count($random_deal);
$random = (rand()%$sizeof);
print(“$random_deal[$random]”);

echo $after_widget;

}
register_sidebar_widget(‘Random Ads’,’widget_random_ads’);
?>


Let’s break it down…

1 – The plugin header
Contains all the information about the plugin, this information is shown at the Plugins page in your WordPress admin area.


<?php
/*
Plugin Name: Random Ads
Version: 0.1
Plugin URI:  
Description: My Random Ads, mainly Think Geek Stuff (by Alex costa)
Author URI: http://itehchlog.com
*/
?>


2 – the Function & the Widget enclosure

echo $before_widget;   opens the widget

echo $bafter_widget;   closes the widget

function widget_random_ads($args) {
extract($args);



echo $before_widget; #this opens the widget

$random_deal[] = ‘<a href=”http://www.anrdoezrs.net/click-4163564-10769259″ target=”_top”>
<img src=”http://www.awltovhc.com/image-4163564-10769259″ width=”250″ height=”250″ alt=”16 Port USB Hub” border=”0″/></a>’;

$random_deal[] = ‘<a href=”http://www.jdoqocy.com/click-4163564-10764658″ target=”_top”>
<img src=”http://www.lduhtrp.net/image-4163564-10764658″ width=”250″ height=”250″ alt=”ThinkGeek Use Your Brain” border=”0″/></a>’;

srand(time());
$sizeof = count($random_deal);
$random = (rand()%$sizeof);
print(“$random_deal[$random]”);

echo $after_widget; #this closes the widget

}



3 – The PHP random and variables (where the ads go).

The $random_deal[] are the arrays that hold the information to be shown, each array stars with $random_deal[] = and the HTML to be output goes inside the single quotation marks ‘ ‘ and the whole statement ends with semicolons ; . Each array contains the HTML a single ad and will be picked at random by the PHP function rand() and shown on the page by the command print(). Note that double quotation marks are allowed inside the single speech marks but if you have a single quotation mark or an apostrophe inside the HTML you must add a slash before it. E.g: alt=”Today’s deals” should be alt=”today\’s deals” .

You can have as many arrays as you want.

$random_deal[] = ‘<a href=”http://www.anrdoezrs.net/click-4163564-10769259″ target=”_top”>
<img src=”http://www.awltovhc.com/image-4163564-10769259″ width=”250″ height=”250″ alt=”16 Port USB Hub” border=”0″/></a>’;


$random_deal[] = ‘<a href=”http://www.jdoqocy.com/click-4163564-10764658″ target=”_top”>
<img src=”http://www.lduhtrp.net/image-4163564-10764658″ width=”250″ height=”250″ alt=”ThinkGeek Use Your Brain” border=”0″/></a>’;

srand(time());
$sizeof = count($random_deal);
$random = (rand()%$sizeof);
print(“$random_deal[$random]”);


4 – Register the widget.

Now you need to call the widget to be added to WordPress. This is a native WordPress function called register_sidebar_widget(); the argument  ‘Random Ads’ is the title of the Widget and ‘widget_random_ads’ is the name of the function above.

register_sidebar_widget(‘Random Ads’,’widget_random_ads’);


5 – Installation.

Upload your random_ads.php file to \wp-content\plugins (preferably create a folder called random_ads and put it inside it) via FTP. Then Go to your WordPress plugin administration and activate the plugin.  Now you can drag and drop the widget into your side bar on the Widget admin area under Appearance.

Download random_ads.zip with source code

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>