Best practices when customizing Joomla with plugins

When comparing Joomla with its main competitor WordPress, it might be that WordPress is easier to use and looks slicker overall. But still Joomla is known to be more flexibable and adjustable - a real CMS for powerusers.

When you gain a little bit of knowledge of PHP, Joomla becomes even more flexible: The architecture is easy to extend, provided that you know what to tune where.

There are numerous extension types to extend Joomla with: Components give the main functionality to pages. Modules allow you to generate output in positions on the side - menus, breadcrumbs, search, etcetera. And plugins do something under the hood. This article is about plugins.

No core overrides ... ever

One of the main aspects of extending Joomla is that there is no need to override the core. Ever. When you want to change the templating output of a core component, the MVC design allows you to override layout files using output overrides. Basically, you create a folder structure in your own Joomla template, copy the layout files to that folder structure and modify them. This prevents the original files from being modified, so you can safely upgrade Joomla whenever it is needed. The same output override principle applies to Joomla modules.

Overriding login behaviour

However, Joomla is even more flexible: Let's say that you want to change the way that a login occurs. Using output overrides, you can modify the login form. But let's say that you want to change the redirect URL that people are being redirected to after they have succesfully logged in. The form itself contains a hidden field "return" which allows for changing this redirect URL. However, imagine that you want to change this redirect URL based upon the outcome of the login itself - for instance, users of the usergroup A should be redirected to a different URL than users of the usergroup B. Changing the form will not be of any help, because the form is displayed at a point in time when the users are not logged in yet.

This is where plugins can come to the rescue: By creating a Joomla plugin, you can hook into the "onUserAfterLogin" event, check for the usergroup of the current user and redirect to the right URL.

Overriding anything

A plugin is even able to override the login controller by rerouting the POST request to another component, which extends from the original login controller. This way you can rewrite any part of a MVC component (Model, View, Controller) without duplicating everything, but simply by extending whatever you need to extend. Plugins are even able to override Joomla core library files. Without the requirement to modify these original files. Ever.

Benefits of using plugins

The main benefit of using plugins is that you can modify things in Joomla without dirty hacks. There are numerous plugin events that allow you to do this. Even better, there are thousands of plugins already out there on the Joomla Extension Directory, which allow you to reuse functionality easily. Sometimes these plugins will do the job, sometimes they don't. By learning about basic PHP code and plugins, you can easily create your own plugins and customize them for your projects. Even better: If you write a specific plugin for every specific task you need to have done, you can start creating your own personal plugin listing for either personal or public use. Plugins are awesome, and once you get the hang of writing them, you will discover a whole new level of Joomla.

Helping you along the way

Personally, I've written a book to help you with developing Joomla plugins the proper way. The book is already seen by many as the authoritive guide for writing plugins - not only because it serves as a full guide for all the plugin events you can find in Joomla, but also because it gives you numerous practical examples to work with. The book is available in printed format and as e-book. Checkout our availability page for details.