Why Every Joomla Sitebuilder Should Learn Plugin Development
Sitebuilders, you know the scenario; a new client has no idea about Joomla and what it does. They want a new web site or web app and they don’t care what CMS you use and they need it do something very specific, something bespoke.
The problem is, the specific functionality is not offered ‘out-of-the-box’ by Joomla or by an extension on the JED. In fact the function is so bespoke that the problem is never likely to be solved by an extension from the JED.
My company had this problem recently when a client needed us to authenticate new site registrations and logins in a very specific way.
The clients’ organisation had over 30,000 off-line members. They issue their members with a new membership card every year with a unique membership code. Members' cards arrive in the post and they use them at the clients’ shops to get “member only” offers.
This was all fine until they decided it was time to get a website and add an on-line dimension to their business.
Joomla had all the functionality they needed, User manager, content manager, contacts manager etc... so when they came to us to build their site we were delighted to offer them a new Joomla website.
The problem was, how do we offer this very specific registration/login functionality with Joomla? How do we make sure that only card holders, with their unique card number, are able to register on the site; and how do we authenticate the specific membership code numbers for each member when they register and login?
In other words, we couldn’t let just anyone register on the site, only card holders with their unique membership code.
Usual sitebuilder Options: search the JED!
After an extensive search around the JED we found there were some solutions that came close to offering what we needed, but none that allowed us to do exactly what we needed.
Another option would be to build a bespoke component to manage all the members, but since Joomla’s native User Manager is more than capable of handling 1000’s users and is native to Joomla’s system, building a new component would be overly complex and time-consuming and would certainly be a case of “re-inventing the wheel”.
So what was the solution? Joomla’s plugin system allows you to add bespoke functionality to the CMS without adding a “core hack”.
(A core hack is when a developer changes code inside the Joomla library. This practice used to be fairly common but it is highly not recommended. Changing core files means that you can no longer update your Joomla system without overwriting your hacked files and therefore breaking the functionality on your site that you needed)
Joomla plugins act upon “events” in the Joomla execution cycle. The plugin “listens” for a particular event and the functionality is triggered when that event takes place.
There are lots of useful “events” that a plugin can listen for, some to do with rendering the content (ie. Before the content is rendered or after the content is rendered) others are to do with saving or deleting content - say you needed a particular action to run every time a user tried to delete an article in the backend - we can use a plugin to do that!
Other events are to do with the System; we can perform actions each time an action is taken in Joomla before anything else happens - we use “onAfterInitialise” for this. We can also perform actions when the execution cycle is finished etc.
The events that we needed to focus on for our plugin was the “User” events group. These events in the system are triggered when a user first registers, logs in, logs out, etc… the system even has an event for when a user enters the wrong username or password.
(I hope at this stage, as a sitebuilder, your creative juices are flowing… think about offering your users bespoke functionality for when they are having trouble logging in to your site… the “onUserLoginFailure” event will allow you to do this! How amazing is that!?!).
The real beauty of Joomla plugin development is, you do not need to be an expert PHP developer to get to grips with it. In fact, plugin development is a great “gateway” into overall Joomla development as you get to know lots of useful code, functions and patterns; all of which will be extremely useful when you move on to modules and then components.
In the case of our bespoke user plugin, since usernames are unique in the Joomla system, we used language overrides to change “Username” to “Membership Code” and then used the “onUserBeforeSave” event to check the users entered “Membership Code” against a preset list of allowed “Membership Codes”.
This meant that only someone registering with a valid “Membership Code Number” would be allowed to register successfully on the site.
The code we had to write for this plugin amounted to 19 lines of PHP and around 30 lines of XML. So with less than 50 lines of code, we managed to solve a massive bespoke problem and allowed Joomla to carry out a specific function. Think about how many lines of code we would have needed to build a component that performed the same functionality!
It is clear from this example, that every sitebuilder should get to grips with plugin development.
Interested in getting started with plugin development? See related article: https://magazine.joomla.org/issue-may-2014/item/2029-introduction-to-plugin-development-for-site-builders