As you know, content plugins for Joomla! mainly deals with content items. It searches the content items and performs some actions on the content. For example, when shubmitting an article, content plugin may check for valid markups and remove invalid markups. Similarly, when displaying the article, another content plugin may perform some activities to display the article in a special format. In this chapter, we will learn how to develop content plugins for Joomla!. In this chapter, you will learn:

  • How to create plugin files
  • How to Install the plugin
  • How to enable the plugin
  • How to add functions to the plugin
  • What are the events for content plugin
  • How to package the content plugins

Get started with the plugin

If you look into structure of a plugin for Joomla 2.5, you’ll find certain files. The first file is definitely a xml file. The xml file contains information sabout the plugin, its author, plugin’s description and files associated with the plugin. This file also defines the plugin parameters or options for the plugin.

Let us start a plugin that will show Hello World! message. The plugin’s name will be ‘Hello World’. So first create a folder named hello_world on your computer’s disk. Then create a helloworld.xml file as follows:

As you can see, we have defined the plugin’s name by <name>..</name> element. Under <files> element we have listed the required files for the plugin. In this case, we have listed two files helloworld.php and index.html. Between these two files, note the plugin="helloworld" attribute for helloworld.php file. It denotes that helloworld.php file is the main file for helloworld plugin.

After creating the XML file, we now need to create our php file (hellowprld.php), which does all of the work for the plugin. Create the helloworld.php file with the following code:

Note the construction of this file. As you know, we have used defined('_JEXEC') or die; to prevent direct access to the file. Later we have defined the class which extends JPlugin class. Later in this chapter, we will be learning about the parameters used in the function.

The last file we need to create is one named index.html. This file is just to prevent directory listing and therefore you don’t need to place any code in the file; simply need to create it.

So after creating all these files, you should have the following files:

As we have created these files inside Joomla! installation, j25dev/plugins/content/helloworld directory, we can discover the plugin and install it. For installing it, in Joomla! administration panel, Extension Manager and click Discover tab. If you don’t see the helloworld plugin listed, click Purge icon and then click Discover icon. That shows the plg_content_helloworld plugin listed.

Discover the Plugin

Select the plg_content_helloworld plugin and click Install icon in the toolbar. That will install the plugin.

If you have created the helloworld plugin folder elsewhere, you can just compress the folder and install it through Extension Manager as any other plugin.

Other posts of the series

  1. Tutorial: Developing Joomla! Content Plugin
  2. Adding parameters to a Joomla 2.5 plugin