User Tools

Site Tools


plugins:creation

This is an old revision of the document!


Plugin Creation

GetSimple plugins allow easy modification, customization, and enhancement to a GetSimple website. Instead of changing the core programming of GetSimple, you can add functionality with our plugin system.

Anatomy of a Plugin

This section describes the different parts of a plugin. For this example, lets use this basic plugin that echos “Hello World” at the bottom of every front-end page. Download Example Plugin

<?php
/*
Plugin Name: Hello World
Description: Echos "Hello World" in footer of theme
Version: 1.0
Author: Chris Cagle
Author URI: http://www.cagintranet.com/
*/

# get correct id for plugin
$thisfile=basename(__FILE__, ".php");

# register plugin
register_plugin(
	$thisfile, //Plugin id
	'Hello World', 	//Plugin name
	'1.0', 		//Plugin version
	'Chris Cagle',  //Plugin author
	'http://www.cagintranet.com/', //author website
	'Finds email addresses in content and components and "hides" them', //Plugin description
	'theme', //page type - on which admin tab to display
	'hello_world_show'  //main function (administration)
);

# activate filter 
add_action('theme-footer','hello_world'); 

# add a link in the admin tab 'theme'
add_action('theme-sidebar','createSideMenu',array($thisfile,'Hello World description'));

# functions
function hello_world() {
	echo '<p>Hello World</p>';
}

function hello_world_show() {
	echo '<p>I like to echo "Hello World" in the footers of all themes.</p>';
}
?>

Variable Setup

This code could always be hardcoded, but it's best if you define the below code right after your registration.

# get correct id for plugin
$thisfile=basename(__FILE__, ".php");

This code is what is used to display your plugin on the “Plugins” tab of the control panel. It also allows you to set the type of page your plugin should be shown as.

# register plugin
register_plugin(
	$thisfile, //Plugin id
	'Hello World', 	//Plugin name
	'1.0', 		//Plugin version
	'Chris Cagle',  //Plugin author
	'http://www.cagintranet.com/', //author website
	'Finds email addresses in content and components and "hides" them', //Plugin description
	'theme', //page type
	'hello_world_show'  //main function
);

Note: $thisfile will not work within hook functions like hello_world or hello_world_show, rather it will be the name of the plugin last loaded.

Hooks & Filters

This code is what attaches your custom function to a particular hook or filter.

# activate filter
add_action('theme-footer','hello_world',array()); 

Syntax: add_action('hook name','function to call',Array of Args);

This code displays a link to a plugin specific page in the administration (it is a special case of add_action):

# add a link in the admin tab 'theme'
add_action('theme-sidebar','createSideMenu',array($thisfile,'Hello World description'));

Syntax: add_action('tab-name-sidebar','createSideMenu',array($thisfile,'link text'));

Note: When this link is clicked, it will display the page type tab and call the main function (both specified in the register_plugin call), thus in general the action (here theme-sidebar) and the page type (here theme) should match.

Custom Functions

The functions in this section would be the functions that are called as part of the registration, a hook or a filter.

# functions
function hello_world() {
	echo '<p>Hello World</p>';
}

function hello_world_show() {
	echo '<p>I like to echo "Hello World" in the footers of all themes.</p>';
}

Standard for File & Folder Creation

Additional plugin files

A simple plugin will consist of only one file, e.g. my_plugin.php. But if your plugin consists of more than this main my_plugin.php file you should put them into a directory my_plugin.

If your plugin supports multiple languages, you should put the language files into a sub folder lang. For more information see Languages (I18N).

Similarly you can have subfolders for javascript files, images, etc. For all directories whose files need to be directly accessed by the browser, add a .htaccess file for Apache with the line

Allow from all

Thus a sample plugin structure for a plugin my_plugin with additional php files and images that supports multiple languages might be:

- my_plugin.php
- my_plugin (directory)
   - .htaccess (with one line: Deny from all)
   - common_functions.php
   - lang (directory) 
      - .htaccess (with one line: Deny from all)
      - en_US.php
      - de_DE.php
      - it_IT.php
   - img (directory)
      - .htaccess (with one line: Allow from all)
      - add.png
      - delete.png
      - edit.png

Data & Settings

If you need to save your data to a file on the server, we recommend saving it to a new folder within the GSDATAOTHERPATH path. For example: If your plugin needs to save the Google Analytics's UA-XXXXX id for the site, it would be best if you saved it within the folder /path/to/getsimple/data/other/my_plugin_folder/ua-data.txt, where /my_plugin_folder/ is the folder you create and ua-data.txt is the file that holds your data.

When saving or accessing files and folders within a GetSimple installation, it is always best to use the defined constants set by the system. You can get the list of contents from the /admin/inc/common.php file, or by looking at our svn copy of it.

plugins/creation.1315377327.txt.gz · Last modified: 2013/04/19 14:56 (external edit)