How to Create Plugin for Joomla 3.x
Plugins are powerful way of extending the functionality of the Joomla. Plugins provide functions which are associated with trigger events. Joomla has a set of core plugin events. Plugin code is executed when certain events are triggered.
Events or plugins are divided into groups.
A basic Joomla plugin contain following files:
- XML Installation File
- PHP Code File
List of Event Plugins
Authentication
- onUserAuthenticate
Captcha
- onInit
- onDisplay
- onCheckAnswer
Content
- onContentPrepare
- onContentAfterTitle
- onContentBeforeDisplay
- onContentAfterDisplay
- onContentBeforeSave
- onContentAfterSave
- onContentPrepareForm
- onContentPrepareData
- onContentBeforeDelete
- onContentAfterDelete
- onContentChangeState
- onContentSearch
- onContentSearchAreas
Editors
- onInit
- onSave
- onSetContent
- onDisplay
- onGetContent
- onGetInsertMethod
Extensions
- onExtensionAfterInstall
- onExtensionAfterUninstall
- onExtensionAfterUpdate
- onExtensionBeforeSave
- onExtensionAfterSave
Finder
- onFinderCategoryChangeState
- onFinderChangeState
- onFinderAfterDelete
- onFinderAfterDelete
- onFinderBeforeSave
- onFinderAfterSave
Quick Icons
- onGetIcons
System
- onAfterInitialise
- onAfterRoute
- onAfterDispatch
- onAfterRender
- onBeforeRender
- onBeforeCompileHead
- onSearch
- onSearchAreas
- onGetWebServices
User
- onUserAuthorisation
- onUserAuthorisationFailure
- onUserBeforeSave
- onUserAfterSave
- onUserBeforeDelete
- onUserAfterDelete
- onUserLogin
- onUserLoginFailure
- onUserAfterLogin
- onUserLogout
- onUserBeforeSaveGroup
- onUserAfterSaveGroup
- onUserBeforeDeleteGroup
- onUserAfterDeleteGroup
How To Start
To get started with plugin development, first you need to decide what problem you are trying to solve or what functionality you are trying to implement. Check the Joomla Extension Directory (JED) that a solution to this problem does not already exist.
The next step is to come up with a unique name for this extension. The first part of the name (plg) indicates that this is a plugin extension. Second part identifies what type of plugin this is. Third part is the unique name. For example, plg_content_uniquename
XML Installation File
This file is similar to manifest files for other extensions. You have to add the group entry in the <extension> tag and the extended information in the <filename> tag. This information tells Joomla! into which folder to copy the file and to which group the plugin should be added.
PHP Code File
In Joomla, all plugins are subclass of JPlugin, a base class that implements the basic properties of plugins. In methods, following properties are available:
- $this->params (parameters set in the administrator)
- $this->_name (name of the plugin)
- $this->_type (group of the plugin)
- $this->db (db object)
- $this->app (application object)
<?php
// no direct access
defined( '_JEXEC' ) or die;
class Plg<PluginGroup><PluginName> extends JPlugin
{
/**
* Load the language file on instantiation.
*/
protected $autoloadLanguage = true;
/**
* Plugin method with the same name as the event will be called automatically.
*/
public function <EventName>()
{
/*
* Plugin code goes here.
* You can access database and application objects and parameters via $this->db,
* $this->app and $this->params respectively
*/
return true;
}
}
?>
As with all extensions in Joomla, you can install plugins as a .zip file.