Difference between revisions of "Creating a Widget"

From WHMCS Documentation

m (added activation after uploading)
Line 3: Line 3:
 
All the widgets included in WHMCS by default are open source, and so are the ideal way to get started by following one of them as an example. You can find the default widgets in the '''/modules/widgets/''' folder.  
 
All the widgets included in WHMCS by default are open source, and so are the ideal way to get started by following one of them as an example. You can find the default widgets in the '''/modules/widgets/''' folder.  
  
The basic idea is that you need to create a function, which returns an array with 2 parts - a title, and the content.  The title is the name displayed for the widget, and the content is the output within the widget block.  And then using the same methodology as with hooks, you define the custom function to WHMCS using a Hook call of "AdminHomeWidgets". So here's a very basic Hello World example:
+
The basic idea is that you need to create a function, which returns an array with 2 parts - a title, and the content.  The title is the name displayed for the widget, and the content is the output within the widget block.  And then using the same methodology as with hooks, you define the custom function to WHMCS using a Hook call of "AdminHomeWidgets".  
  
<source lang="php">
+
<div class="docs-alert-info">
<?php
+
<span class="title">Note</span><br />
 
+
Sample code can be found at [http://developers.whmcs.com/addon-modules/admin-dashboard-widgets/ http://developers.whmcs.com/addon-modules/admin-dashboard-widgets/]
function widget_hello_world($vars) {
+
</div>
 
 
    $content = '<p><strong>Hello World</strong> Sample Widget Content</p>';
 
 
 
    return array( 'title' => 'Hello World', 'content' => $content );
 
 
 
}
 
 
 
add_hook("AdminHomeWidgets",1,"widget_hello_world");
 
 
 
?>
 
</source>
 
 
 
An additional variable return that is supported from the widget functions is "jquerycode" and using that, it is possible to define jQuery scripting that is then placed in the <head> section of the page, and this can be used for having ajax based loading of content within the widget. Please refer to the '''whmcs_news.php''' widget within the /modules/widgets/ folder for a good example of this functionality.
 
 
 
Right now the AdminHomeWidgets is the only available hook point, but in due course we plan to add support for other pages where widgets will be able to be utilised.
 
 
 
When a new widget is first installed into the system, the first thing you will always need to do before you can see & use it is navigate to your admin role group setup area as described above and enable it. Once enabled, you can then navigate back to your homepage and the new widget will appear as the top widget on your dashboard which you can move around and adjust as desired.
 

Revision as of 10:52, 4 January 2017

Creating a widget is very easy to do. They can be defined as part of any hook file - be it part of a provisioning/server module, registrar module or addon module. Or even just a standalone hook file for a standalone widget on it's own.

All the widgets included in WHMCS by default are open source, and so are the ideal way to get started by following one of them as an example. You can find the default widgets in the /modules/widgets/ folder.

The basic idea is that you need to create a function, which returns an array with 2 parts - a title, and the content. The title is the name displayed for the widget, and the content is the output within the widget block. And then using the same methodology as with hooks, you define the custom function to WHMCS using a Hook call of "AdminHomeWidgets".