Cpanel Plugin Templating Basics

Recently I picked up a side project creating a WP-Engine-Esq cPanel plugin to handle staging environment and pushing to production and during this gig I pretty much found that there is not enough documentation on creating custom plugins for cPanel. As the motto of my blog is “Sharing is Caring” here I am to help the lonely programmer who is tasked for writing a cPanel plugin. Writing cPanel plugin templates has little documentation and of that which exists is rarely 100% correct. So I want to step through the basics of writing a simple AJAX supported cPanel plugin.

So lets show you Cpanel Plugin Templating of a simple plugin that allows you to delete domains through the UAPI. First we want to ensure we share the same layout as the rest of cPanel. At the time of this writing were going to likely wrongly assume you are using the paper_lantern theme.

So to match everything we are going to start with:

[% WRAPPER '_assets/master.html.tt' -%]

This is going to wrap your code in the base theme defined in ‘/usr/local/cpanel/base/frontend/paper_lantern/_assets/master.html.tt’ so you don’t have to worry about any of the header.

And because when I write code I like to write my “END” blocks prior to filling the content lets go ahead and do that. Add some blank new lines (your preference) and add the following line:

[% END %]

Both of these are template toolkit code, for more details see: Template Toolkit Manual

So this is going to handle the header and footer of your template making it match everything else in your cPanel. At this point we are going to assume that in the assets file that bootstrap and all that jazz is included and build a pretty bootstrapy plugin.

We write a simple table with a list of e-mail addresses for all domains using the UAPI. I will comment as necessary.

<table class="table table-striped">
  <thead>
    <tr>
      <th>Domains</th>
      <th>Delete</th>
    </tr>
  </thead>
  <tbody>
    <!-- This executes a UAPI api call to the cpanel api module DomainInfo with the method 'list_domains' and returns an hash of hash of arrays of addon, parked, main and sub domains -->
    [% data = execute( 'DomainInfo', 'list_domains' ); %]
    <tr>
      <!-- here we loop through all of the addon domains -->
      [% FOREACH var = data.data.addon_domains %] <!-- Loop through addon_domains ie hash of hash of arrays -->
      <!-- lets assume we loop through all the other domains the same way -->
      <td>[% var %]</td> <!-- display email -->
      <td><span class="delete_domain">Delete Domain</span></td>
      [% END %]
    </tr>
  </tbody>
</table>

This will create our table within our template that contains each domain name and provides a delete button. This is nothing cool but provides the basics and shows a simple API call.

From here all you would need to do is write a jQuery or Javascript call to handle on click events on $(“.delete_domain”) and pass the domain variable to either a template API call or backend API call whichever you prefer.

Write a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.