How to add and use WHMCS Hooks
WHMCS hooks are a powerful feature that enables you to extend and customize the behavior of the WHMCS system by executing custom PHP code at specific events or actions. These events, known as hook points, occur throughout the WHMCS workflow and provide opportunities for developers to intervene and introduce custom functionalities.
2. Creating a Hook File:
- Create a PHP file for your hooks. This file will serve as a container for your custom code. For example, let’s name it
custom_hooks.php
.
3. Adding Hooks to the File:
- Define your hooks in the
custom_hooks.php
file. Ensure that the file checks if WHMCS is defined to prevent unauthorized access.
<?php
if (!defined(“WHMCS”)) {
die(“This file cannot be accessed directly”);
}// Add a hook for the ‘ClientAdd’ event
add_hook(‘ClientAdd’, 1, function($vars) {
// Your custom code here
logActivity(‘Client Added: ‘ . $vars[‘userid’]);
});// Add a hook for the ‘InvoicePaymentReceived’ event
add_hook(‘InvoicePaymentReceived’, 1, function($vars) {
// Your custom code here
logActivity(‘Invoice Payment Received: ‘ . $vars[‘invoiceid’]);
});
?>