<

How to create a PlugIn bootstrap in PHP from scratch (no frameworks)

Published on
9,746 Points
3,646 Views
1 Endorsement
Last Modified:
Approved
This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing systems such as WordPress. Joomla, Drupal, etc., as they already have their own plugin bootstraps.

This article assumes you have basic PHP knowledge.

Each plugin will be in it's own folder by the same name as the plugin. For example:
   If your plugin is named  "example", then you would create a folder in your "plugins" directory, named "example", and a file in your "plugins / example" directory name "example.php".
   Additionally, any functions (or classes) inside "example.php", should be preceded with the name of the plugin:   example_init() { }

-------------------------------------------------------------------------------------------------------
STEP 1.

This is where we write the code for /index.php . This is the core file, and will contain all the logic as to what we want to load inside the plugins, including any functions we wish to support (or classes as you can expand on this code later).

/index.php
<?php
// Set the path of the plugin folder
define('PATH_PLUGINS','plugins');

$plugin_path = "." . DIRECTORY_SEPARATOR . PATH_PLUGINS . DIRECTORY_SEPARATOR;
if ($handle = opendir($plugin_path)) {
    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) {
		switch($file) {
			case ".":
			case "..":
				break;
			default:
				if(is_dir($plugin_path . $file)) {
					// INITIALIZE PLUGIN HERE
					$plugin_name = $file;
					$plugin_load = $plugin_path . $file . DIRECTORY_SEPARATOR . $file . ".php";
					if(file_exists($plugin_load)) {
						try {
							// CHECK IF PLUGIN INITIALIZED OR CONFLICTING INIT FUNCTION
							if(!function_exists($file . '_init')) {
								include($plugin_load);
								
								// CHECK IF PLUGIN_INIT() FUNCTION EXISTS AND EXECUTE IT
								if(function_exists($file . '_init')) {
									eval($file . '_init();');  // Execute the INIT function in the plugin;
								}
							}
						} catch (Exception $e) {
							echo "PLUGIN [" . $file . "] FAILED TO LOAD: " . $e->getDescription();
						}
					} else {
						echo "PLUGIN [" . $file . "] MISSING BOOTSTRAP.";
					}
				}
				break;
		}
    }
    closedir($handle);
}
?>

Open in new window



STEP 2

This is example "plugin" code. Name all of the functions inside the plugin using the following naming convention:
PLUGINNAME_FUNCTIONNAME

Not only does this keep the plugin names unique, but they are easier to call when you need them. If you have multiple plugins (the whole point of writing a plugin system), then each file that is loaded will add functions (or classes) to the currently running scripts pool.

/plugins/example/example.php
function example_init() {
	try{
		// Do some stuff here
		echo "PLUGIN INITIALIZED";
		
	} catch (Exception $e) {
		// Save debug to log file or whatever -- returns false as init failed.
		return false;
	}
	return true;
}

function example_displaystuff() {
	echo "This should be displayed if the function exists";
}

Open in new window


You can continue to add more functions using the same naming convention. Additionally, when the plugin is loaded, you can choose to check for each function you wish to access and add them to an array/database as to whether they exist or not.

To call a function inside your plugin,
eval($plugin_name . '_yourfunctionname();');

Open in new window


If there is a function that requires parameters in your plugin that you wish to call:
eval($plugin_name . '_yourfunctionname(' . $myparam1 . ',' . $myparam2 .');');

Open in new window


There are several variations on writing this code as PHP is a dynamic language, however I feel this is the simplest method to create a functional plugin system for scripts that otherwise do not have one.
plugin-bootstrap.zip
1
Comment
Author:microvb
0 Comments

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Join & Write a Comment

Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month