<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

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

Published on
9,786 Points
3,686 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

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Join & Write a Comment

The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month