How to create module in Joomla step by step

Before knowing about how to create a module in Joomla step by step from the backend you should know the Joomla module.

What is the Module of Joomla?

Sometimes we need to display a small block of information like the newsflash, Login Form, Slider, etc. So in Joomla, this type of block is called Modules. Joomla module paced according to positions of the template.

Let See: How do I install quickstart packages in Joomla?

Create Module in Joomla step by step

In this tutorial, I will show how you can create a custom module for Joomla3.x and Joomla4.x.

Please follow the following steps –

Step1: Create directory structure with mod_latestposts

There are two ways to create a directory in your Joomla website module folder or in your computer local folder.

If you will create it on your local computer then you will need to create a zip file and install it by the Joomla Extension manager.

Now I am starting from the Joomla website root directory then selecting the Modules folder.

Let’s create a module: mod_latestposts

Note: Module root folder name must be started from “mod_”(eg. mod_modulename) also the main file must be the same naming convention.

  • Create inside the module directory of your Joomla website the following folders:
  • /public_html/modules/mod_latestposts
  • /public_html/modules/mod_ latestposts /tmpl
  • /public_html/modules/mod_ latestposts /language
  • /public_html/modules/mod_ latestposts /language/en-GB

Above directories must be followed the same naming convention this important rule.

Let See: How do I enable Joomla PHP error reporting?

Step2: Create file mod_ latestposts.php

This is the main file of the module and its name same as the directory name mod_latestposts.php inside the mod_latestposts.

Joomla Module name file
Joomla Module name file where module functional call by helper Class

The defined directive limits permit the rest of the file or code from any other non-Joomla scripts. In other words, if it’s not from Joomla standard code or out of the Joomla directory, It will  “die” or stop all further action.

JLoader::register-  This function load the file that included in the path. For example above loader function have helper.php

JLoader::register(‘ModPostsLatestHelper’, __DIR__ . ‘/helper.php’);

You can use require_once() in the place of loader ::register() for example as below-

require_once dirname(__FILE__).’/helper.php’;

But in the latest version of Joomla is using the Loader() function.

Step3: Create file mod_ latestposts.xml

Joomla Modules xml installation file
Joomla Modules XML installation file

Above the screenshot, there is some important fields ex. Name, Author, creationDate, etc

Files Section

To add files in our module you need to add these fields: <filename></filename>

And to add folders use this tag <folder></folder>. And these all fields tags should be inside the <files></files> fieldset.

Module file tag in xml
Module file tag in XML
Language Section

To add language file you need to use <languages> directives and inside this use <language></language> directive like below –

Language tag in xml
Language-tag in XML

The section includes language files and a language folder(ex. en-GB). When you will install the module these language files will be placed within the language folder.

For your module, you can also add a custom language folder. The benefit of a custom folder is that it will not be removed by updates, and your module will be self-contained.

Configuration Section
Joomla4: How do I create a custom module in Joomla?
Joomla4: How do I create a custom module in Joomla?

Configuration settings start from the config tag, and inside this tag, we can add multiple fieldsets like Basic, Advanced, etc.

In the basic fieldset, we add parameters for the module function and the Advanced fieldset is used for default options of the Joomla module.

Step #4. Create Module helper.php

In this file you have to create a helper.php file with helper class for example, in our module helper class is – ModPostsLatestHelper

You have to use the Camel case for a naming convention for the class name.

In fact, the helper.php file isn’t required to run a module. If you browse the other module folders, you’ll notice that several of them are missing. However, for this example, we’ll need to write one.

What is the use of this helper.php?

You included a line requiring the helper.php file when you will write the mod_latestposts.php file. Actually, we write database action methods in this file.

 How do I create a custom module in Joomla?
How do I create a custom module in Joomla?

If you include this, but you don’t have it, Joomla is going to give you an error message that the file wasn’t found. If you don’t want to use one, don’t use the need to require in the mod_latestposts.php file.

Helper file methods – in the helper.php file you have to write your function or methods are you will need to get data from database tables. In our module, I have created the getposts()   method to get the latest posts from the content table.

Module function in Joomla
Module function in Joomla

Let See: How to create override for Joomla layout in 2022?

Step #5. Check all files and folders –

Module files structure in joomla3
Module files structure in joomla3

Now we can install the module from the administrator. SO first you have to go administrator and click on the System menu on the left side. There you can see in middlebox Install .where you can see the Discover link and click on it.

 How do I create a custom module in Joomla?
Discover the module from the Joomla backend

Let See: Joomla4: How to create a new module from the backend?

After that you will see module mod_latestposts there, just click on the Install button, and then it will install in Joomla.

How to install Joomla module from admin
How to install Joomla module from admin

You have to check the checkbox for which module you want to install. After installation, you will see a successful message.

Now You have to Go to Module Manager=>New Button

In the module type list click module: mod_latestposts

Create new module from admin
Create a new module from the admin

Like with other modules, you have to enter module title, select module position,  menu assignment, etc.

In our module, we have to set the article(post) category and the number of posts on the front end.

And click on Save & Close button.

Display module in the sidebar
Display module in the sidebar

Now, this is the final output of the module. On the right side, you can see posts.

You may like to read these also: What is the Joomla control panel?

Related Posts