PHP Plugin & Hook System

PHP Plugin & Hook System

Released 5 years ago , Last update 5 years ago

The PHP Plugin & Hook System allows you to implement a plugin system into your current or next PHP powered application or framework

There are many that covet the plugin systems of the most popular content management systems such as WordPress, Joomla, Drupal, etc. Even some of the most popular frameworks have a plugin system, but what if you don't use a framework? Where do you turn?

Now you can turn to the PHP Plugin and Hook System. It is Object Oriented and should be easy to integrate into any PHP application to allow for third party or user extensions or plugins. With this system you can:

  1. Register action hooks & filters
  2. Load activated plugins
  3. Retrieve plugin's information
  4. Override action filters
  5. Register plugin admin pages
  6. Register options
  7. and more ...

Pricing

14 day 14-day money-back guarantee

$29.99

Single Site License

  • Perpetual license

  • 1 site, 1 server

  • No distribution (hosted use only)

  • Commercial use

$199.99

Developer License

  • Perpetual license

  • Unlimited projects

  • Can distribute code and binary products

  • Commercial use

  • 12 months support

Installation

Follow the steps below for installation.

  • In the zip file, you will see a folder named package. In the package folder you will find config.inc.php. Edit this file with your database details as well as the url to where you will install the package folder.
  • In the package folder you will also find a file named table.sql. Import this into your database.
  • Once you have done the steps above, visit your installation (i.e. http://example.com/package)
  • If the installation went well, you will see the plugin panel with the two plugins that are installed. That's it you are done. What is to follow is a break down of the plugin class and how to use it.

Usage

When using the class, you should follow the same file structure as the downloaded package.

  • config.inc.php - Variables for your database as well as the base url and plugin directory.
  • panel.php - Lists the plugins that are installed on your system.
  • activate.php - Is called when you want to activate a particular plugin.
  • deactivate.php - Is called when you want to deactivate a particular plugin.
  • init.php - Used to automatically load activated plugins. The menu calls and other action calls are located in this file.
  • options.php - This is used to print out the settings page for an activated plugin. You will need to secure this for the administrator.
  • user_page.php - Prints out an activated plugin that is to be used by your users.

Plugin Header

The class reads the main php file for header information that is printed when viewing the plugin panel. The only one that is not printed is Plugin Slug. This is utilized for building in an upgrade system. More on this later on.

/*
Plugin Name: Plugin Name
Plugin URI: http://www.example.com/plugin_name
Version: 1.0
Description: A short description of the plugin.
Author: Author Name
Author URI: http://www.example.com/
Plugin Slug: plugin-name
*/

// PHP code and action hooks/filters.

Plugin File Structure

There is a certain structure when it comes to developing plugins. Each plugin must have its own folder which is based on a slug naming convention and the main plugin file must end with the following extension ".plugin.php".

Following the following structure for when creating a plugin:

plugin-name // name of the plugin folder
----plugin-name.plugin.php // main plugin file
----plugin-name.txt // plugin slug file

The plugin slug file is based on the name of the main plugin folder. In this file it contains two things, the plugin slug and the version of the plugin.

plugin-name
1.0

If you use SVN or GIT to allow third parties to submit plugins, you can use this information to build your own update system that will allow you to compare the newly updated slug file with the one that is currently installed on a user's server.

Activating/Deactivating Plugin

When you activate a plugin, the location of the plugin will be inserted into the database. This will be used for file inclusion. When you deactivate the plugin, the information in the database for the plugin will be deleted and the file will no longer be included throughout the system.

activateplugin($plugin) / deactivateplugin($plugin)

Parameters:

  1. $plugin - (string) (required) The Plugin Filename

Usage:

hooks::activate_plugin($plugin);

hooks::deactivate_plugin($plugin);

Example:

hooks::activate_plugin('plugin_name');

hooks::deactivate_plugin('plugin_name');

Retrieving Options

You can retrieve saved plugin options from the database.

getoption($optionname, $default = false)

Parameters:

  1. $option_name - (string) (required) Name of the option to retrieve from the database.
  2. $default - (mixed) (optional) Returned false if no option is found.

Usage:

hooks::get_option( $option_name, $default = false );

Example:

//Retrieve Site's Title
echo hooks::get_option( 'sitetitle' );

Adding Options

Saving plugin settings/options to the database.

add_option( $name, $value = '' )

Parameters:

  1. $name - (string) (required) The name of the option to be added to the database.
  2. $value - (mixed) (required) The value for the option.

Usage:

hooks::add_option( $name, $value = '' );

Example:

hooks::add_option( 'alternate_email_address', 'test@email.com' );

Updating Options

Just like saving options to the database, you can also update the options.

updateoption( $optionname, $newvalue )

Parameters:

  1. $option_name - (string) (required) Name of the option to update.
  2. $newvalue - (mixed) (required) The new value used to update the option_name.

Usage:

hooks::update_option( $option_name, $newvalue );

Example:

$option_name = 'geo_location';
$new_value = '40.39689449999999, -70.13304649999999';

if ( isset($_POST[$option_name]) ) {
        $value = db::inst()-escape( $_POST[$option_name] );
        hooks::update_option( $option_name, $new_value );
}

Deleting Options

When developing plugins, you can also use a method that will allow you to delete options fron the database when a plugin is deactivated.

delete_option( $name )

Parameters:

  1. $name - (string) (required) Name of the option to be deleted

Usage:

hooks::delete_option( $name );

Example:

hooks::delete_option( 'sitetile' );

Action Hooks

Action hooks are run during a certain request. You can define more action hooks in the init.php file. Let's say that you wanted to add a header action hook. With a header action hook, you can include other css or javascript files.

addaction($hook, $function, $priority = 10, $acceptedargs = 1)

Parameters:

  1. $hook - (string) (required) Name of the hook
  2. $function - (string) (required) Name of the function to call
  3. $priority - (int) (optional) Defines when this action should be loaded
  4. $accepted_args - (int) (optional) Defines the number of arguments to accept

Usage:

hooks::do_action( $hook, $arg = '' );

Example:

// Define new action in init.php
function header() {
    hooks::do_action( 'header' );
}

// Lines found in plugin file
function new_css_file() {
    echo '';
}
hooks::add_action( 'header', 'new_css_file' );

Adding Filters

Performs a filtering operation on a PM element or event. Returns an element which may have been filtered by a filter.

addfilter( $hook, $function, $priority = 10, $acceptedargs = NULL )

Parameters:

  1. $hook - (string) (required) The name of the filter to hook the $function to.
  2. $function - (string) (required) A callback for the function to be called when the filter is applied. E.g. "myemail_address".
  3. $priority - (int) (optional) Used to specify the order in which the functions associated with a particular action are executed (default=10, lower=earlier execution, and functions with the same priority are executed in the order in which they were added to the filter)
  4. $accepted_args - (int) (optional) The number of arguments the function accept (default is the null).

Usage:

hooks::add_filter( $hook, $function, $priority = 10, $accepted_args = NULL )

Example:

$fromEmail = 'myname@gmail.com';

function myemail_address() {
return $fromEmail;
}
hooks::add_filter( 'mail_from', 'myemail_address' );

Applying Filters

Performs a filtering operation on an element or event.

apply_filter( $hook, $value = '' )

Parameters:

  1. $hook - (string) (required) The name of the an element or action.
  2. $value - (mixed) (optional) The value of the element before filtering.

Usage:

hooks::apply_filter( $hook, $value = '' );

Example:

$custom_redirect = hooks::apply_filter( 'redirect', 'http://example.com/custom_page/', '302' );

Removing Filters

This function removes a function attached to a specified filter hook. This method can be used to remove default functions attached to a specific filter hook and possibly replace them with a substitute.

removefilter( $hook, $functiontoremove, $priority = 10, $acceptedargs = 1 )

Parameters:

  1. $plugin - (string) (required) The filter hook to which the function to be removed is hooked.
  2. $functiontoremove - (string) (required) The name of the function which should be removed.
  3. $priority - (int) (optional) The priority of the function (default: 10).
  4. $accepted_args - (int) (optional) The number of arguments the function accepts (default: 1).

Usage:

hooks::remove_filter( $hook, $function_to_remove, $priority = 10, $accepted_args = 1 );

Example:

hooks::remove_filter( 'comment_email', 'send_comment_email' );

Plugin Pages

There two ways to link to plugin pages. If they have settings/options, then you would use listpluginadminpages(); if the page if for your users then you would use listpluginuserpages(); There are also two methods to use that will allow you to register this pages in your plugin.

registeradminpage( $slug, $title, $function, $icon_url = '' )

Parameters:

  1. $slug - (string) (required) Will be used for the $_GET['page'] variable.
  2. $title - (string) (required) Used when printing out the url
  3. $function - (string) (required) Call the function that will print out the page.
  4. $icon_url - (string) (optional) Add an icon to use in the url or use the default.

Usage:

hooks::register_admin_page( $slug, $title, $function, $icon_url );

Example:

hooks::add_action( 'admin_menu', 'plugin_admin_page', 10 );

function plugin_admin_page() {
    // parameters: page slug, page title, and function that will display the page itself
    hooks::register_admin_page( 'admin_page', 'Test Admin Page', 'admin_do_page' );
}

function admin_do_page() {
    // HTML code that will be called by the function above for the admin page.
}

registeruserpage( $slug, $title, $function, $icon_url = '' )

Parameters:

  1. $slug - (string) (required) Will be used for the $_GET['page'] variable.
  2. $title - (string) (required) Used when printing out the url
  3. $function - (string) (required) Call the function that will print out the page.
  4. $icon_url - (string) (optional) Add an icon to use in the url or use the default.

Usage:

hooks::register_user_page( $slug, $title, $function, $icon_url );

Example:

hooks::add_action( 'user_menu', 'plugin_user_page', 10 );

function plugin_user_page() {
    // parameters: page slug, page title, and function that will display the page itself
    hooks::register_user_page( 'user_page', 'Test User Page', 'user_do_page' );
}

function user_do_page() {
    // HTML code that will be called by the function above for the user's page.
}
3 licenses, starting from From » $29.99 View Licenses

Get A Quote

What do you need?
  • Custom development
  • Integration
  • Customization / Reskinning
  • Consultation
When do you need it?
  • Soon
  • Next week
  • Next month
  • Anytime

Thanks for getting in touch!

Your quote details have been received and we'll get back to you soon.


Or enter your name and Email
  • HW Henry Withoot License holderSingle Site License
    Purchased on May 21, 2014
    4 years ago
    This is a very limited script , i play 5 minutes with it and already can see that. Also misses any documentation inside the downloaded .zip no readme, no pdf, nothing. Also misses requirements. It not tell to get the script working, as it comes, that you should enable the gettext extension on your server. Can i get my money back... i promise i trow it in the bin.
    • JP Joshua Parker Publisher 4 years ago
      You should contact Binpress.
    • NA Naseer Ahmad 3 years ago
      How useful is this with any custom development?
  • ZN zak naji 4 years ago
    Is any of the code encrypted.
    • JP Joshua Parker Publisher 4 years ago
      No, it is open source and Binpress only supports open source projects.
    • ZN zak naji 4 years ago
      Thanks for the quick answer and reply.