Plugin development guide #2: Hooks Plugin development guide #2: Hooks

Using and understanding hooks

The Cotonti plugins system is based upon the simple principle of including blocks of code inside the core files. If you look into these core files, which are located in system/core/.../, you will find blocks of code like this:

$extp = sed_getextplugins('index.main');
if (is_array($extp))
	{ foreach($extp as $k => $pl) { include_once($cfg['plugins_dir'].'/'.$pl['pl_code'].'/'.$pl['pl_file'].'.php'); } }

The ‘index.main’ code is the unique hook code. The hook looks up a list of plugins using this hook code, and executes the corresponding plugin files with a simple include_once. Knowing this, you will be able to better understand the way your plugin works and interacts with the Cotonti core. You should treat the plugin code as if it were part of the core code, replacing the hook code. This means that you can use and modify core variables and use global Cotonti functions. This also means you can use the $usr[] variable for accessing data concerning the visitor, which is mostly useful for granting or denying access. The downside is that you need to be aware of naming conflicts. For example, most single character variable names are in use by the Cotonti core. This means it is not safe to use these variable names inside your plugin, because you might overwrite a system variable, introducing new bugs and errors.

The order by which plugin files are included by a hook is defined by the plugin. In most cases it is okay to use the default value (10), but in some special cases you will want to have control over the order by which the plugin code is included. This will allow you to make two plugins successfully work together. The plugin file with the lowest Order value will be included first.

No comments yet
Only registered users can post new comments