Troubleshooting Hooks

During the development process, you may need to verify whether your hooks are working correctly. This can include checking whether hooks are loading properly, triggering the desired hook point, and running the expected hook code.

You may want your hook to only run in certain situations (for example, only running for specific products). Make sure that you check for these scenarios when verifying your code.

Check Hook Loading

To check whether your hooks are loading properly, enable debugging for them.

Enabling debugging will result in a large number of Activity Log entries. We recommend only enabling this while you are actively debugging code.

To do this:

  1. Go to the Other tab at Configuration () > System Settings > General Settings.
    The Other tab in General Settings
  2. Check Hooks Debug Mode.
  3. Click Save Changes.
  4. Perform the action that should trigger the action hook point.
  5. Go to Configuration () > System Logs.
  6. Examine the most recent log entries for Hook File Loaded messages:
    Hook debugging entries in the System Logs
For more information, see our Logging Developer Documentation.

Check Hook Triggering

If your hooks are loading properly, you can proceed to checking whether the hook point is triggering.

To do this:

  1. Enable Hooks Debug Mode (if it is not already enabled) and perform the hook action (above).
  2. Go to Configuration () > System Logs.
  3. Examine the most recent log entries for Called Hook Point messages:
    Hook debugging entries in the System Logs

In the example above, the system successfully called the AdminAreaFooterOutput hook point.

Check Hook Execution

If your hooks are loading properly and triggering, you can proceed to checking whether your code is executing.

To do this, you can use the logActivity() function. This method allows you to debug the results to check which variables are present when the hook triggers:

  1. Add the following line at the end of your code:

    logActivity('Hook variables: '.print_r($vars, true));

    For example:

    <?php
    
    if(!defined('WHMCS')) {
        die('This hook should not be run directly');
    }
    
    add_hook('AdminAreaFooterOutput', 1,function($vars) {
        logActivity("AdminAreaFooterOutput hook has run. Posted Vars: ". print_r($vars, true));
    
    });
    
  2. Enable Hooks Debug Mode (if it is not already enabled) and perform the hook action (above).

  3. Go to Configuration () > System Logs.

  4. Examine the most recent log entries. The variables will display in the log entries.

Module Hooks

Typically, the system will load hook files on every page load.

  • Exact hook behavior depends on the specific module.
  • The system triggers hook code every time a module command runs for any module for which the hook is active.

Hook Changes After Module Activation

The system detects hook files when you activate the module. If the module is already active when you add or modify a hook file, you will need to perform additional steps:

  • For server and provisioning modules, go to the Module Settings tab for an applicable product at Configuration () > System Settings > Products/Services and click Save Changes.
  • For domain registrar modules, go to that domain registrar at Configuration () > System Settings > Domain Registrars and click Save Changes.
  • For addon modules, go to that addon module at Configuration () > System Settings > Addon Modules and click Save Changes.
For more information, see Module Hooks.

Last modified: October 30, 2024