How to create a cPanel plugin

How to create a cPanel plugin

You don’t need to be a system administrator or a cPanel expert to create your own cPanel plugin. Anyone who can write PHP code should have no trouble building a plugin. Within a day, I have created my first plugin: LastLogin – which outputs the content of the .lastlogin file.
You can complete this task in roughly 20 minutes using this how-to create a cPanel plugin tutorial.

The first thing that you need is a good idea for a plugin, in this example, I will create a cPanel plugin that shows the content of the .lastlogin file to cPanel users.

.lastlogin is a hidden (dot file) that keeps a log of the last 20 IPs that have successfully logged to the cPanel account.

image 33 - How to create a cPanel plugin

Creating a cPanel plugin from an existing PHP script

The simple PHP code that I will use for this plugin:

<table id="table" class="styled-table">
<thead>
<tr>
    <th>IP Address</th>
    <th></th>
<th>Date</th>
<th>Time</th>
    <th></th>
  </tr>
</thead>

<?php

$file = fopen("/home/username/.lastlogin", "r") or die("Unable to open file!");    

while (!feof($file)){   
    $data = fgets($file); 
    echo "<tr><td>" . str_replace(' ','</td><td>',$data) . '</td></tr>';
}
echo '</table>';
fclose($file);
?>

I will create a new folder loginlog for plugin files and inside it, a file named index.live.php that will host the actual code.

The first thing I have to add is the cpanel PHP file:

require '/usr/local/cpanel/php/cpanel.php';

to add plugin name on top I use:

$cpanel = new CPANEL();
print $cpanel->header( "Login Log" );

To detect a username and add it to the path, I will use:

class Account
{
    public static function name($cpanel)
    {
        return $cpanel->cpanelprint('$user');
    }

$accountName = Account::name($cpanel);

and then add the $accountName in the file path:

$file = file("/home/$accountName/.lastlogin");

To add cPanel footer add this to the end off the code:

echo $cpanel->footer();
$cpanel->end();

Now you should have a file like this:

<?php
require '/usr/local/cpanel/php/cpanel.php';

//detect username
class Account
{
    public static function name($cpanel)
    {
        return $cpanel->cpanelprint('$user');
    }
}


$cpanel = new CPANEL();
//plugin name on top of page
print $cpanel->header( "Login Log" );

//get username
$accountName = Account::name($cpanel);

//add username to file path
$file = file("/home/$accountName/.lastlogin");

?> 
<table id="table" class="styled-table">
<thead>
<tr>
    <th>IP Address</th>
    <th></th>
<th>Date</th>
<th>Time</th>
    <th></th>
  </tr>
</thead>

<?php

$file = fopen("/home/$accountName/.lastlogin", "r") or die("Unable to open file!");    

//output the content of .lastlogin file
while (!feof($file)){   
    $data = fgets($file); 
    echo "<tr><td>" . str_replace(' ','</td><td>',$data) . '</td></tr>';
}
echo '</table>';
fclose($file);
?>

<?php

    //add capenl footer
echo $cpanel->footer();
$cpanel->end();


Adding a plugin to cPanel

So let’s go ahead and add this file in cPanel. Plugin files are stored in the theme directory:

/usr/local/cpanel/base/frontend/paper_latern/
/usr/local/cpanel/base/frontend/jupiter/

So if you want the plugin to be available on the Jupiter theme, add it to its folder, or if you want it to be available on both themes, add it to both folders.


Testing a new cPanel plugin

Now you can test the plugin by going to cPanel and editing the URL link to match the newly created directory:

https://domain.com:2083/cpsessXXXXX/frontend/paper_lantern/loginlog/index.live.php
loginlog plugin folder
index.live.php script

The plugin should look like this:

image 32 - How to create a cPanel plugin

Make sure to double-check for code errors, as if there are any, the cPanel will output an error:

image 31 - How to create a cPanel plugin

Using cPanel plugin generator

After I have added the files, I need to Install the plugin so that a plugin icon with a link to the script is created in that Panel template. I will need a name and icon (48x48px) for the plugin and a cpanel group to add it under.

WHM has a convenient cPanel plugin generator page where you can generate needed .json files for the plugin.

image 34 1024x517 - How to create a cPanel plugin

Once you fill in all the fields, click on the button Add item, then fill in the plugin name and click on Generate.

image 35 - How to create a cPanel plugin


This will download plugin-name.tar.gz which contains:

install.json icon name, plugin dir, plugin name, group_id and order
meta.json time when the plugin was generated and cp version
loginlog.png icon for the plugin

Installing a cPanel plugin

To install this plugin in cPanel:

//create a new directory for the plugin
mkdir -p /usr/local/cpanel/base/frontend/paper_lantern/loginlog

//download plugin files 
wget https://github.com/stefanpejcic/lastlogin-cpanel-plugin/archive/refs/heads/main.zip

//extract
unzip main.zip

//move them to the new folder
mv index.live.php /usr/local/cpanel/base/frontend/paper_lantern/loginlog
mv src/Account.php /usr/local/cpanel/base/frontend/paper_lantern/loginlog/src
mv  loginlog.tar.gz /usr/local/cpanel/base/frontend/paper_lantern/loginlog

//install the plugin from the .tar.gz file
/usr/local/cpanel/scripts/install_plugin /usr/local/cpanel/base/frontend/paper_lantern/loginlog/loginlog.tar.gz --theme paper_lantern

You should now see a new icon Login Log in the Security section in cpanel.

image 36 - How to create a cPanel plugin

The complete source code of this plugin can be downloaded from GitHub: https://github.com/stefanpejcic/lastlogin-cpanel-plugin

whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.