How To Create Custom WHMCS Module

To create a simple custom WHMCS module, you will need to create a folder for your module and add the following files:

  1. config.php: This file contains your module’s configuration options and settings.
  2. client.php: This file contains the functions and logic for the client-side of your module.
  3. admin.php: This file contains the functions and logic for the admin-side of your module.

Here is an example of a basic structure for a WHMCS module:

Copy codeyourmodulename/
├── config.php
├── client.php
└── admin.php

In the config.php file, you will need to define your module’s name, version, author, and other configuration options. Here is an example of a basic config.php file:

Copy code


<?php
// Define your module's name and version
$_ADDONLANG['name'] = 'Your Module Name';
$_ADDONLANG['version'] = '1.0';
// Define the module's author and additional information
$_ADDONLANG['author'] = 'Your Name';
$_ADDONLANG['description'] = 'A short description of your module';
$_ADDONLANG['usage'] = 'Instructions on how to use your module';
// Define your module's configuration options
$_ADDONLANG['configoption1'] = 'Option 1';
$_ADDONLANG['configoption2'] = 'Option 2';

In the client.php and admin.php files, you will need to define the functions and logic for your module. You can use WHMCS hooks, template variables, and API functions to interact with the WHMCS system and perform tasks such as creating custom pages, adding menu items, and interacting with client and order data.

Here is an example of a basic client.php file that adds a custom page to the client area:

Copy code


<?php
use WHMCSModuleAddonYourAddonNameClientTemplate as ClientTemplate;
// Define a function to generate the custom page output
function yourmodulename_output($vars)
{
    // Get the client's name and email address
    $clientName = $vars['client']['firstname'] . ' ' . $vars['client']['lastname'];
    $clientEmail = $vars['client']['email'];
    // Generate the custom page output
    $output = '<p>Hello ' . $clientName . '!</p>';
    $output .= '<p>Your email address is ' . $clientEmail . '.</p>';
    return $output;
}
// Add the custom page to the client area using the ClientAreaPage hook
add_hook('ClientAreaPage', 1, function($vars)
{
    // Check if the custom page URL is requested
    if (App::getCurrentFilename() === 'yourmodulename') {
        // Set the page title and call the output function
        $page = array(
            'pagetitle' => 'Your Module Name',
            'breadcrumb' => array('index.php?m=yourmodulename' => 'Your Module Name'),
            'template

Or Here you can go with this simply way

Here is a basic structure that you can use for your module:

Copy code/modules/addons/YourModuleName/
├── config.php
├── lang
│   └── english.php
├── client.php
├── hooks.php
└── templates
    ├── yourtemplate.tpl
    └── yourtemplate2.tpl

Here is a brief explanation of each file and folder:

  • config.php: This file defines the basic information and configuration options for your module. It should contain a config array with the following keys: name, description, author, version, and fields.
  • lang/english.php: This file contains the language strings for your module. It should contain an $_LANG array with the language strings that you want to use in your module.
  • client.php: This file contains the functions that will be called when a client interacts with your module. These functions can be called from the client area or via API calls.
  • hooks.php: This file contains the functions that will be called when certain events occur in WHMCS. These functions are called using WHMCS hooks.
  • templates: This folder contains the templates that will be used to render the output of your module. These templates can be called from the client area or via API calls.

Here is an example of a simple config.php file for a module:

Copy code


<?php
$config = [
    'name' => 'Your Module Name',
    'description' => 'A description of your module',
    'author' => 'Your Name',
    'version' => '1.0',
    'fields' => [],
];
return $config;

And here is an example of a simple lang/english.php file for a module:

<?php
$_LANG['your_language_string'] = 'Your Language String';

For Client

client.php:

Copy code


<?php
use WHMCSModuleAddonYourAddonNameClientTemplate as ClientTemplate;
function your_function_name($vars)
{
    // Your code here
    $output = '<p>Your output</p>';
    // Render the output using a template
    $template = new ClientTemplate();
    $template->assign('yourVariable', $yourVariable);
    return $template->render('yourtemplate');
}

hooks.php:

Copy code

<?php
use WHMCSModuleAddonYourAddonNameClientTemplate as ClientTemplate;
add_hook('ClientAreaHomepagePanels', 1, function($vars)
{
    // Your code here
    $output = '<p>Your output</p>';
    // Create a new panel with the output
    $panel = array(
        'title' => 'Your Panel Title',
        'description' => $output,
        'icon' => 'fa-icon',
        'order' => '4',
    );
    // Add the panel to the client area
    $template = new ClientTemplate();
    $template->addToTemplate('homepagePanels', $panel);
});

yourtemplate.tpl:

Copy code

{$yourVariable}

Note that these are just basic examples and you will need to modify them to fit your specific needs. The client.php file should contain the functions that will be called when a client interacts with your module, while the hooks.php file should contain the functions that will be called when certain events occur in WHMCS. The templates are used to render the output of your module and can be called from the client area or via API calls.

I hope this helps! Let me know if you have any questions or need further assistance.

Categorized in:

WHMCS,

Last Update: December 12, 2023