Tutorial on Creating a Joomla 3 Plugin

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

In this tutorial, we will create a plugin called “shortcode” for Joomla 3.2 which will do a simple text replacement of ‘[copyright]’ with ‘Copyright © 2014’

This saves a bit of typing.  This is also a contrived example in order to keep the tutorial simple for learning purposes.  The concept is similar to the use of shortcodes in WordPress.

shortcode plugin in action

shortcode plugin in action

1.  A plugin requires an xml file which will named “shortcode.xml”.  We put this file within a “shortcode” folder within the “content” directory, which is within the “plugins” directory.  We put this plugin into the “content” directory because we categorize this plugin in the group=”content”.

You can see what the full xml file detail structure looks like in the Joomla docs.  To keep things simple, we have excluded the optional <languages> tag and not put any config parameters.

We will write the following content into shortcode.xml…

xml file for the plugin

xml file for the plugin

The attribute method=”upgrade”  means that this plugin can be installed without uninstalling an earlier version. All existing files will be overwritten, but old files will not be deleted.

The version attribute in the extension tag is the version of Joomla in which this plugin is meant for.   The version tag is the version of your plugin.

2. Second we need the main plugin php file which we will name “shortcode.php” in the same “shortcode” folder.   Put the following content in shortcode.php

plugin php code

plugin php code

The class is named plgContentShortcode in order to follow the convention of …

plg<PluginGroup><PluginName>

We named our event onContentPrepare because that is the exact spelling of an event listed in the possible list of Plugin events here.  Plugin method with the same name as the event will be called automatically.  The parameters for this function is documented here.

3. Create an empty index.html file with the following content…

<!DOCTYPE html><title></title>

This is so that users can not browse the plugin directory.  They will end up seeing an empty white page.

4. Zip the files into shortcode.zip

5. Install the plugin by going to “Extension -> Extension Manager” and upload the shortcode.zip

6.  Activate the plugin by going to “Extension -> Plugin Manager”.

7.  Put the [copyright] shortcode in an article and save.  See that it renders the full text …

shortcode plugin in action

shortcode plugin in action