There may be times where you want to take a specific action when an event occurs in the system. This is called a hook and WHMCS allows you to use their API to write hooks that take action when something occurs in the system. One example would be if you wanted to send a Slack notification when a domain was ordered you could do that with a system like this.
Hooks live inside files located within the /includes/hooks/ directory of your WHMCS install (which is typically found in a “manage” folder of a DoOO install). Walking through the example at https://developers.whmcs.com/hooks/getting-started/ is a great way to learn to write your first hook.
WHMCS provides an index of “Hookable Events” which are opportunities to execute code at certain points where an event occurs in the system. Going back to our example of getting a Slack notification when an order is placed you might use the ShoppingCartCheckoutCompletePage hook for such a task. It’s important to note that certain hooks provide different types of data so you’ll want to review the documentation to make sure you have access to the variables you need. Some hook points will also allow you to return values, and in some cases, the response you provide can alter the program flow to allow you to override default behaviors.
When writing your hook you have the option of writing code that executes an external action (again our Slack notification would be a good example of that) or using the WHMCS API to write an internal task (when a hosting account is terminated add a note to the client’s account for example).
Because WHMCS is a PHP application hooks are written in PHP. Let’s look at the beginning of a sample hook:
* Register hook function call.
* @param string $hookPoint The hook point to call
* @param integer $priority The priority for the given hook function
* @param string|function Function name to call or anonymous function.
* @return Depends on hook function point.
add_hook(‘ClientAdd’, 1, function ($vars)
$userid = $vars[‘userid’];
$firstname = $vars[‘firstname’];
$lastname = $vars[‘lastname’];
$email = $vars[’email’];
$password = $vars[‘password’];
// Run code to create remote forum account here…
This code block is running on the hookable event ClientAdd and retrieving specific variables when a new client is created in WHMCS. With those variables one could then write the necessary code to create an account somewhere else like a support forum using a different API.
Hooks can be a powerful way of extending the functionality of WHMCS to trigger multiple actions on an event. Be sure to review the documentation at https://developers.whmcs.com/hooks/ to learn more about how to build out your own hooks.