You can create your own plugin for use. This is to be installed via the extensions >> install/uninstall in the admin menu. Plugins reside within plugins/<specific category> eg. the plugins for the content is to be found in plugins/content folder. Suppose we create an example plugin of "customloadmodule".
Every plugin requires at least two files the xml file which contains the plugins information and the php file which contains the plugins code. Here are the two files. Note the file naming conventions.
plugins/content/customloadmodule.xml
This file contains the information for the plugin.It also contains the parameters passed and used by the plugin.Here is a sample file.
<?xml version="1.0" encoding="utf-8"?>
<install version="1.5" type="plugin" group="content">
<name>Content - Custom Load Modules</name>
<author>Subhajit Basu</author>
<creationDate>November 2005</creationDate>
<copyright>Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.</copyright>
<license>http://www.gnu.org/licenses/gpl-2.0.htmlGNU/GPL</license>
<authorEmail>subhajit@techshu.com</authorEmail>
<authorUrl>>
<version>1.5</version>
<description>This plugin replaces the modules writtent in articles.</description>
<files>
<filename plugin="customloadmodule">customloadmodule.php</filename>
</files>
<params>
<param name="enabled" type="radio" default="1" label="Enable Plugin" description="PARAMENABLED">
<option value="0">No</option>
<option value="1">Yes</option>
</param>
</params>
</install>
plugins/content/customloadmodule.php
This file conatins the coding for the plugin.
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
// register a hook for plugin,the event name being onPrepareContent .thus when the content is prepared for output then the function plgContentCustomLoadModule() is run
$mainframe->registerEvent( 'onPrepareContent', 'plgContentCustomLoadModule' );
function plgContentCustomLoadModule( &$row, &$params, $page=0 )
{
$db =& JFactory::getDBO();
if ( JString::strpos( $row->text, 'loadmodule' ) === false ) {
return true;
}
$plugin =& JPluginHelper::getPlugin('content', 'customloadmodule');
$regex = '/{loadmodule\s*.*?}/i';
$pluginParams = new JParameter( $plugin->params );
if ( !$pluginParams->get( 'enabled', 1 ) ) {
$row->text = preg_replace( $regex, '', $row->text );
return true;
}
preg_match_all( $regex, $row->text, $matches );
$count = count( $matches[0] );
if ( $count ) {
plgContentCustomProcessPositions( $row, $matches, $count, $regex );
}
}
function plgContentCustomProcessPositions ( &$row, &$matches, $count, $regex )
{
for ( $i=0; $i < $count; $i++ )
{
$load = str_replace( 'loadmodule', '', $matches[0][$i] );
$load = str_replace( '{', '', $load );
$load = str_replace( '}', '', $load );
$load = trim( $load );
$modules = plgContentCustomLoadPosition( $load );
$row->text = str_replace($matches[0][$i], $modules, $row->text );
}
$row->text = preg_replace( $regex, '', $row->text );
}
function plgContentCustomLoadPosition( $position)
{
$contents = '';
$moduleobj = &JModuleHelper::getModule($position);
$contents = &JModuleHelper::renderModule($moduleobj);
return $contents;
}
Here is the actual structure of the file as specified in the joomla site:
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
// Import library dependencies
jimport('joomla.plugin.plugin');
class plg<PluginGroup><PluginName> extends JPlugin
{
/**
* Constructor
*
* For php4 compatability we must not use the __constructor as a constructor for
* plugins because func_get_args ( void ) returns a copy of all passed arguments
* NOT references. This causes problems with cross-referencing necessary for the
* observer design pattern.
*/
function plg<PluginGroup><PluginName>( &$subject )
{
parent::__construct( $subject );
// load plugin parameters
$this->_plugin = JPluginHelper::getPlugin( '<GroupName>', '<PluginName>' );
$this->_params = new JParameter( $this->_plugin->params );
}
/**
* Plugin method with the same name as the event will be called automatically.
*/
function <EventName>()
{
$app = &JFactory::getApplication();
// Plugin code goes here.
return true;
}
}
?>
Using Plugins in Your Code
Now that you've created your plugin, you will probably want to call it in your code. You might not: the Joomla! core has a number of built-in events that you might want your plugin code to be registered to. In that case you don't need to do the following.
If you want to trigger an event then you use code like this:
$dispatcher =& JDispatcher::getInstance(); $results = $dispatcher->trigger( '<EventName>', <ParameterArray> );
It is important to note that the parameters have to be in an array. The plugin function itself will get the parameters as single values. The return value will consist of an array of return values from the different plugins (so it can also contain multilevel arrays).
If you are creating a plugin for a new, non-core event, remember to activate your plugin after you install it. Precede any reference to your new plugin with the JPluginHelper::importPlugin() command.
Also read the detailed discussion of how to create content plugin: http://docs.joomla.org/How_to_create_a_content_plugin