How simple plugin works in Joomla!

While other extension types always have a visual element to them, plugins do not. While they can be used to modify the output of other extensions, their role is more of a behind-the-scenes one. The plugin we are installing now is designed to modify the output of the core Joomla content component. So let's install it.

Let's go to the Backend, just like the other Extensions, go to the Extension Manager, and then browse for the plugin.

Installing plugin in joomla backend

Plugin Successfully Installed

After installing it, go to the Plug-In Manager and here we are going to enable the plugin. It's a content plug-in and it appears right here at the top. So click the Enabled button and that has now published the plugin in the system.

Plugin Screen Manager

What this plugin is designed to do is to search and replace on content and add a link in automatically.Now, if we go back to the Frontend and go to Home, and hit Refresh, you'll notice that we have the content highlighted as a link, We didn't add this link the plugin created this link after seeing the word place.

plugin changing it to link

So how did it do this? Let's go back to the folder of the Joomla installation and go down to plugins.

Navigating to content plugin in joomla

Under here, we want to go to the content plugin folder because this was a content plugin that we installed and we are going to go to the places folder. Inside here is a PHP file and an XML file.

Plugins for places

Let's take a look at the PHP file first. You'll notice here that the main thing that's being defined is a class called plgContentPlaces.

  1. <?php
  2. defined( '_JEXEC' ) or die;
  4. class plgContentPlaces extends JPlugin
  5. {
  6. public function onContentPrepare($context, &$article, &$params, $page = 0)
  7. {
  8. $cities = array('/(place)/i');
  9. $url = JRoute::_('index.php?option=com_noidacity&view=places');
  10. $replacement = '<a href="' . $url . '">$1</a>';
  11. $article->text = preg_replace($cities, $replacement, $article->text);
  12. }
  13. }


So the type of plugin that's being defined is the first thing that comes after the PLG. Then the name of the plugin itself comes after the plugin type. After that, we have a function called onContentPrepare. The onContentPrepare function gets fired at the same time that the onContentPrepare event is fired. Any plugins that have an onContentPrepare function will get run at that point. All this plugin is doing is searching for the word place in the content of the article and replacing that with the URL that is pointing to the places view of the Noidacity component.

In addition to the PHP file, there is an XML file and if we open this, it's just like the modules.xml file. It has things that identify what the plugin is, who wrote it, and what version it is? And it also has a description of what the plugin does.

You'll notice up here at the top that the group is specified as the content group of plugins and that the type of extension is a plugin. Also, down here is a listing of the files that are included as a part of the plugin. Then finally, there is an empty element here for config. This element is typically used to add configuration for the plugin. But at the moment, we just have a very simple plugin. So we're going to leave it empty.

places xml

So this plugin is always going to run whenever the content component runs. Plugins are the best way of adding code you always want to run. You can have them listen for certain events, then silently perform tasks in the background at just the right moment.

Download the Plugin - download