Plugin Best Practices
The most important thing about plugins is to know when to use them. That means understanding the events available in Joomla and what standard behaviors you can override. In many cases, when you are having a difficult time figuring out how to solve a problem with a module or a component, a plugin might make the job a lot easier.
Here are some other tips about plugins:
- Plugins are executed in the order in which they appear in the Ordering column in Plugin Manager. In most cases, the ordering doesn’t matter. However, in some cases, where you have more than one plugin triggered from the same event, and where the results of one plugin can affect the processing of a subsequent one, the order can be important. In this case, you can change the order in the Plugin Manager by adjusting the Ordering values to control the execution order.
- Normally, we want to use the naming conventions for plugin class names and method names. Otherwise, the plugins won’t get called correctly. The exception is if you want the script to be run when the plugin is imported, in which case only the file and folder name is important (as in the class override example in the previous section).
- Different events require different method signatures. Make sure you are aware of what values are available for each event and what values, if any, your method should return. The Joomla repository includes a folder called tests/plugins. This folder contains example plugins for each plugin type. These example files show all the events and the method signatures for each. Also, the plugin events are documented in the Joomla wiki at http://docs.joomla.org/Plugin/Events.
- Try to pick the best event for the job at hand. If you need a new event—for example, in a custom component—you can create it just by triggering it at the desired place in the program. If you believe that you need a new event added in a core class, ask about it on one of the development lists. If others agree, the event can be added to the core.