Our Help Portal

Troubleshooting Support Articles
Searchable Help Information Repository
Self-Service Resources How-Tos
Technical Customer Walkthroughs   Browse All Knowledge ! ....

Get Prices Learn More

Breaking

< All Topics
Print

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:

  1. 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:

  1. 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’]);
});
?>

  1. In this example:
    • ClientAdd: This hook is triggered when a new client is added.
    • InvoicePaymentReceived: This hook is triggered when a payment is received for an invoice.

4. Uploading the File:

  1. Upload your custom_hooks.php file to the WHMCS includes/hooks directory. This directory is the designated location where WHMCS looks for hook files.

5. Testing Your Hooks:

  1. Perform the actions that trigger your hooks. For instance, add a new client through the WHMCS admin interface or make a payment on an invoice.
  2. Check the WHMCS activity log. If you’ve included log statements in your custom code, review the activity log in WHMCS to confirm whether your hooks are being executed.

Important Notes:

  • PHP Syntax: Ensure your custom hook file adheres to proper PHP syntax rules.
  • Cautious Implementation: Exercise caution when implementing hooks to avoid unintended consequences. It’s advisable to test thoroughly in a development environment before applying changes in a production setting.
  • Documentation: Refer to the WHMCS documentation for specific hook points and available variables associated with each hook. Stay updated on any changes or additions to hook points in newer versions of WHMCS.

This detailed guide should provide a comprehensive understanding of how to add and implement WHMCS hooks, from creating the hook file to testing and considering best practices. Always consult the official WHMCS documentation for the latest and most accurate information.

Categories