Example plugin

This is example plugin which will add small advertisement to the footer of your website. It consists of two files, iu-footer-ad.plugin.php, which is main plugin file, and iu-footer-config.php, which is used to configure plugin. Whole plugin can be placed in one file, but it is easier to understand how plugins works if we split it in several files.

This plugin is named “IU Footer AD” and it is easy to understand: We need to attach our function which will show AD to the ”iu_page_footer” hook. Because we want plugin to be easily configurable, we will make configuration panel and place it in iu-footer-config.php file and we will keep all options stored in Instant Update's “settings” MySQL table.

Please place all plugin files in folder manage/plugins/iu-footer/

iu-footer-ad.plugin.php

This is main plugin file. Here, we will only create function which will display AD and attach it to page footer. Also, we will add link to configuration panel below dashboard. Please read all comments in PHP code (lines starting with double slash).

<?php
 
//set plugin id as file name of plugin
$plugin_id = basename(__FILE__);
 
//some plugin data
$data['name'] = "IU Footer ad";
$data['description'] = "Advertise Instant Update in the footer of your website.";
$data['author'] = "Instant Update team";
$data['url'] = "http://www.instant-update.com/";
 
//register plugin to IUPS
if (function_exists('register_plugin')) {
	register_plugin($plugin_id, $data);
}
 
//plugin function which will display AD in footer using settings from MySQL table
function iufooterad_add2footer() {
 
	if (!GetSettingsValue('iu_footer_url')) { $iu_footer_url = 'http://www.instant-update.com/'; }
	else { $iu_footer_url = GetSettingsValue('iu_footer_url'); }
 
	if (!GetSettingsValue('iu_footer_image_alt')) { $iu_footer_image_alt = 'Powered by Instant Update'; }
	else { $iu_footer_image_alt = GetSettingsValue('iu_footer_image_alt'); }
 
	if (!GetSettingsValue('iu_footer_image')) { $iu_footer_image = './manage/images/iu_signature.gif'; }
	else { $iu_footer_image = GetSettingsValue('iu_footer_image'); }
 
	if (!GetSettingsValue('iu_footer_image_align')) { $iu_footer_image_align = 'right'; }
	else { $iu_footer_image_align = GetSettingsValue('iu_footer_image_align'); }
 
	if (!GetSettingsValue('iu_footer_image_target')) { $iu_footer_image_target = '_blank'; }
	else { $iu_footer_image_target = GetSettingsValue('iu_footer_image_target'); }
 
	if (GetSettingsValue('iu_footer_active') != 'no') {
		echo "\n<p align='$iu_footer_image_align'><a href='$iu_footer_url' target='$iu_footer_image_target' title='$iu_footer_image_alt'><img src='$iu_footer_image' border='0' alt='$iu_footer_image_alt' /></a></p>\n";
	}
}
 
//plugin function which will add link to configuration panel below dashboard
function iufooterad_add2dash() {
	if (iu_is_admin()) {
		add_menu_item("Configure footer Ad", "plugins/iu-footer/iu-footer-config.php", IU_LINK_POPUP, "Footer AD administration", 500, 450);
	}
}
 
//attach plugin functions to a hooks
if (function_exists('add_hook')) {
	add_hook('iu_page_footer','iufooterad_add2footer');
	add_hook('below_dashboard','iufooterad_add2dash');
}
?>

iu-footer-config.php

This is configuration panel of plugin. It is also used if you want to uninstall plugin. Please read all comments in PHP code (lines starting with double slash).

<?php
//Define BASE_PATH. It is needed for file connection.php so don't forget to define it and point it to manage/ directory.
//Here, manage/ directory is two directories above current directory (manage/plugins/iu-footer/)
define('BASE_PATH', '../../');
 
//Include connection.php, needed for connection to database
include BASE_PATH . 'connection.php';
 
//Include all functions used in Instant Update
include BASE_PATH . 'methods.php';
 
//Allow only administrators to access this page
if (!iu_is_admin()) { die("Administrators only!"); }
?>
<html>
<head>
<title>IU footer Ad configuration</title>
</head>
<body>
 
<h1>IU footer Ad configuration</h1>
 
 
<?php
//Uninstallation subroutine
if (isset($_GET['uninstall'])) {
	RemoveSetting('iu_footer_url');
	RemoveSetting('iu_footer_image_alt');
	RemoveSetting('iu_footer_image');
	RemoveSetting('iu_footer_image_align');
	RemoveSetting('iu_footer_image_target');
	RemoveSetting('iu_footer_active');
	die("<p>IU footer Ad data removed! Now you can remove folder iu-footer from your plugins directory!</p>");
}
 
//Subroutine used to save changes
if (isset($_POST['save'])) {
	WriteSettingsValue('iu_footer_url', $_POST['iu_footer_url']);
	WriteSettingsValue('iu_footer_image_alt', $_POST['iu_footer_image_alt']);
	WriteSettingsValue('iu_footer_image', $_POST['iu_footer_image']);
	WriteSettingsValue('iu_footer_image_align', $_POST['iu_footer_image_align']);
	WriteSettingsValue('iu_footer_image_target', $_POST['iu_footer_image_target']);
	WriteSettingsValue('iu_footer_active', $_POST['iu_footer_active']);
	echo "<p>IU footer Ad data saved!</p>";
}
 
?>
 
<form action="#" method="post">
<input type='hidden' name='save' value='yes' />
<p>Ad URL: <?php 
//If there is no setting (it is false), set the default value
if (GetSettingsValue('iu_footer_url') == false) {
	WriteSettingsValue('iu_footer_url', 'http://www.instant-update.com/','text');
}
 
//Display HTML form field
echo CreateSettingFormField('iu_footer_url');
?> </p>
 
<p>Ad ALT text: <?php 
if (GetSettingsValue('iu_footer_image_alt') == false) {
	WriteSettingsValue('iu_footer_image_alt', 'Powered by Instant Update','text');
}
echo CreateSettingFormField('iu_footer_image_alt');
?> </p>
 
<p>Ad image URL: <?php 
if (GetSettingsValue('iu_footer_image') == false) {
	WriteSettingsValue('iu_footer_image', './manage/images/iu_signature.gif','text');
}
echo CreateSettingFormField('iu_footer_image');
?> </p>
 
<p>Ad image align: <?php 
if (GetSettingsValue('iu_footer_image_align') == false) {
	WriteSettingsValue('iu_footer_image_align', 'right','select','left|right|center');
}
echo CreateSettingFormField('iu_footer_image_align');
?> </p>
 
<p>Ad link target: <?php 
if (GetSettingsValue('iu_footer_image_target') == false) {
	WriteSettingsValue('iu_footer_image_target', '_blank','select','_blank|_self|_top');
}
echo CreateSettingFormField('iu_footer_image_target');
?> </p>
 
<p>Ad active: <br /> <?php 
if (GetSettingsValue('iu_footer_active') == false) {
	WriteSettingsValue('iu_footer_active', 'yes','radio','yes|no');
}
echo CreateSettingFormField('iu_footer_active','<br />');
?> </p>
 
<p><input type="submit" value="Save" /></p>
</form>
 
<p><a href='?uninstall=yes' onclick='return confirm("Are you sure?");'>Uninstall plugin</a></p>
</body>
</html>
 
plugins/example_plugin.txt · Last modified: 2008/07/15 11:07 by avram
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki