7 minutes reading time (1414 words)

How to Create a Joomla! Extension with the Help of AI

October-Extension-AI

Nine months ago, I created my first Extension for Joomla!. Now, I’ve made seven simple Extensions with the help of ChatGPT. In this article, I’ll share what I learned through this process in the form of tips so that you can easily create your own Extension.

Whether you’ve been working with Joomla! for a short while or many years, creating an Extension might seem overwhelming. But with this practical guide, you'll discover that, thanks to Large Language Models (LLMs), it’s now easier than ever to develop a simple Extension that meets your specific needs.

Let me start by saying I don't consider myself a programmer. I’ve been professionally involved in IT for almost 20 years, but my programming skills are quite limited. I mention this to reassure you, as it would be a bit hypocritical to claim it’s easy to create an Extension while being a programmer. Having clarified this, of course, let me also say that dealing with Joomla! since it was called Mambo, over the years I've had the opportunity to familiarize myself with how it works, the logic behind building Extensions and the ways they connect to the Joomla! core, but also to watch in awe the incredible development which this popular CMS had over time.

That said, having some basic knowledge of PHP, CSS, JavaScript, and HTML helps a lot when you want to tailor a Joomla! website to your needs and evaluate the responses AI tools like ChatGPT provide. Even if you don't have this knowledge, I hope the following tips give you a good foundation for building your first Joomla! Extension.

Give Clear and Detailed Instructions

This is a key principle in IT, in general. Just like client requirements need to be clear, Large Language Models (LLMs) need precise and clear instructions to generate the best possible outcome. The quality of ChatGPT’s responses depends on the clarity of the questions posed. This is where “prompt engineering” comes into play, an evolving IT field where professionals design precise instructions to guide AI platforms toward the desired output. So, the clearer your instructions, the better your results.

Use the Right Tools

Have you ever watched a DIY video where someone promises to teach you how to build a chair in 5 minutes, but sooner or later you realize that they can only do it because they happen to have hundreds or even thousands of dollars’ worth of the right equipment in their garage? I won’t assume you can afford a paid subscription to the latest version of ChatGPT. However, having used both the free and paid versions, I must say the difference is significant in terms of the scope, speed, and accuracy of responses. For instance, when I needed help with creating a mobile-friendly CSS grid with Bootstrap, the free version didn’t solve my problem after 30 minutes of prompting, but the paid version did it in 5 seconds.

Moreover, the free version (until recently) didn’t have updated information about Joomla!. At one point, I asked if the Extension I was building followed Joomla! 5 best practices, and it replied that it would have more information once Joomla! 5 was released (even though Joomla! 5.0.3 had already been released). However, it would be remiss of me not to mention the huge progress that the free version has also made in recent months.

Another advantage of the paid version is access to custom GPTs like JoomGPT, which specialize in answering Joomla! related questions. But bear in mind, even these GPTs can sometimes miss out on specifics.

Use the Tools, Right (by following Best Practices for Joomla!)

While the other advices given in this article could apply to any CMS or platform in general, this one focuses on Joomla! and its specific technical features. One of the most important things to remember is that ChatGPT often gives a solution without considering Joomla!'s best practices. For instance, Joomla! uses the Model-View-Controller (MVC) software architecture for its Extensions, but ChatGPT might give you a simpler solution that doesn’t follow this structure unless you specifically ask for it.

 

Example: Hello World Module

Let’s say you ask ChatGPT to create a simple Module that displays “Hello World” on your site. ChatGPT will probably generate:

  • A mod_helloworld.php file with code like:
    echo '<h1>Hello World!</h1>';
  • A mod_helloworld.xml file with the necessary information for installing the Module.

 

Will it work? Yes, but it ignores several Joomla! principles and best practices, such as:

  • Joomla! uses the MVC architecture, where the Model manages data, the View presents the information, and the Controller handles user input and commands.

In our "Hello World" example, the message display belongs to the View and should be in a tmpl/default.php file. The mod_helloworld.php file acts as the Model and calls the View with a command like require ModuleHelper::getLayoutPath('mod_helloworld');. For a simple Module like this, Model and Controller may overlap in one file, but the structure should look like this:

mod_helloworld/
mod_helloworld.php
mod_helloworld.xml
tmpl/default.php

  • While “Hello World” itself is a simple and clear message, what if the message becomes more complex and/or needs to be displayed in different languages? A good practice is to use variables for both Front-end and Back-end messages. So, in the default.php file, you would write:
echo '<h1>' . Text::_('MOD_HELLOWORLD_MESSAGE') . '</h1>';

where “Hello World” is now the content of the variable 'MOD_HELLOWORLD_MESSAGE' which is defined in a new file, at the path language/en-GB/en-GB.mod_helloworld.ini with the content:

MOD_HELLOWORLD_MESSAGE="Hello World"
  • In Joomla! 5, several functions have been simplified, e.g. JText::_() function becomes Text::_() and more generally the prefix “J” has been removed from such functions. Additionally, the language file en-GB.mod_helloworld.ini no longer needs to have the language prefix 'en-GB', as it is located in the relevant /en-GB/ folder anyway. ChatGPT might give you outdated code, so if you want your Extension to be Joomla! 5 native, ask for it specifically. In any case you definitely want to be very careful not to end up using legacy code for a new Extension (unless, of course, you're developing an Extension for an older version of Joomla!).

  • ChatGPT’s example might have used an h1 tag to display "Hello World" but this limits customization. A better practice would be to assign a class to the text:
echo '<div class="hello-world-message">' . Text::_('MOD_HELLOWORLD_MESSAGE') . '</div>';

So you can then style this class in your CSS, for example:

.hello-world-message {
color: #3498db;
font-size: 2rem;
}

 

Ask for Help

Even a seemingly simple Extension like the "Hello World" Module can be tricky if it's your first time developing a Joomla! Extension. This is why it’s crucial to seek help from the Joomla! community whenever you encounter issues. For instance, during my first Extension’s development, a simple removal of a single word in my code, suggested by a coworker (a programmer who in fact was unfamiliar with Joomla!), resolved a bug that troubled me for days. Later, a member of the Joomla! community introduced me to some best practices about Joomla! that even ChatGPT didn’t “know” at the time. Finally, you can also look at the source code of Extensions from other developers or even Joomla! itself in order to get a better idea of ​​how an Extension is structured or how a particular function is implemented. For example, I had to look at a number of third-party Extensions to understand how they integrate with the Joomla! Update System by using a Github repository as the update server.

Share Your Work

If you’re creating an Extension for Joomla!, it’s probably either for learning purposes because you want to get more involved or because no existing Extension meets your needs. Whatever the case, publishing your Extension, preferably under an open licence (like Joomla!'s), can have many benefits. First, it might help someone else who might have been looking for a similar Extension. Second, the community can test it and suggest improvements, thus evolving your creation. And third, I probably don't need to tell you how great it feels to share your creation and see it being used by different people, on different sites. Besides, to share an Extension you don't even need any special infrastructure. By creating, for example, a Repository of the Extension on Github, you can not only share but also maintain the Extension with upgrades, fixes, etc. Additionally, if you feel ready to seriously and responsibly support this Extension, you can also submit it to the Joomla! Extensions Directory, the place where everyone searches for Joomla! Extensions.

Some articles published on the Joomla Community Magazine represent the personal opinion or experience of the Author on the specific topic and might not be aligned to the official position of the Joomla Project

1
The Joomla Academy: an opportunity for students to...
Joomla! for Large Projects: Success Strategies and...
 

Comments 2

Already Registered? Login Here
Brendan Hedges on Sunday, 20 October 2024 15:26
Frustrations!

Hi Ioannis,

Well done, a nice article.

I'd like to add, that asking ChatGPT to help is a great idea, but don't expect it to be plane sailing! I find it's like arguing with a petulant child! And, forgetful!! OMG, the amount of hours I've wasted trying to get results that not only fulfil Joomla's requirements, but also include content long since accepted as "working" in new versions is time consuming.

I too would recommend getting a paid subscription as it allows much more time using the advanced gpt's over the free versions.

However, regardless of the restrictions and annoyances I've started to use ChatGPT for making modules and plugins for specific jobs if I can't find them already in the JED. They might not be 100% compliant, but they work.

The journey continues!

1
Hi Ioannis, Well done, a nice article. I'd like to add, that asking ChatGPT to help is a great idea, but don't expect it to be plane sailing! I find it's like arguing with a petulant child! And, forgetful!! OMG, the amount of hours I've wasted trying to get results that not only fulfil Joomla's requirements, but also include content long since accepted as "working" in new versions is time consuming. I too would recommend getting a paid subscription as it allows much more time using the advanced gpt's over the free versions. However, regardless of the restrictions and annoyances I've started to use ChatGPT for making modules and plugins for specific jobs if I can't find them already in the JED. They might not be 100% compliant, but they work. The journey continues!
ANDI on Monday, 21 October 2024 03:21
Claude much better

We use claude is much better than chatGPT when it comes to Joomla dev code

0
We use claude is much better than chatGPT when it comes to Joomla dev code

By accepting you will be accessing a service provided by a third-party external to https://magazine.joomla.org/