How to Find All Available Variables in a WHMCS Hook

When developing a custom WHMCS hook, one of the first questions you may encounter is:

Which variables are actually available inside this hook?

WHMCS passes an array of variables to hook functions, usually through the $vars parameter. The variables available depend on the specific hook point being executed and, in some cases, on the context in which the hook is triggered.

Although the WHMCS documentation lists the parameters for many hook points, during development and debugging it can be useful to see the actual $vars array received by the hook.

Fortunately, there is a very simple way to do this using the WHMCS Activity Log.

How WHMCS Hook Variables Work

A typical WHMCS hook looks like this:

<?php

add_hook('HookName', 1, function($vars) {

    // Your code here

});

The $vars array contains the parameters passed by WHMCS to that particular hook.

Its contents vary depending on the hook point. A domain-related hook, for example, may receive domain, registrar and client information, while an invoice hook will receive parameters related to the invoice or payment.

Display All Variables Available in a WHMCS Hook

The WHMCS documentation on hooks is rather concise, so the best way to find out exactly which variables WHMCS passes to a specific hook is to write the complete $vars array to the WHMCS Activity Log.

For example, suppose you want to inspect the variables available in the PreRegistrarRegisterDomain hook.

Create a temporary hook containing:

<?php

add_hook('PreRegistrarRegisterDomain', 1, function($vars) {

    logActivity(json_encode($vars), 0);

});

When the hook is triggered, json_encode() converts the complete $vars array into JSON.

The resulting data is then written to the WHMCS Activity Log using:

logActivity(json_encode($vars), 0);

You can view the result from the WHMCS administration area.

The log entry will contain the variables passed to the hook together with their current values.

Making the Output Easier to Read

Depending on the hook, the $vars array can contain a considerable amount of data.

Since json_encode() produces the output on a single line, a large array may be difficult to read directly from the WHMCS Activity Log.

You can copy the JSON output and paste it into a JSON formatter or parser to obtain an indented, structured representation.

For example, instead of something similar to:

{"domain":"example.com","registrar":"example","userid":123,"params":{"example":"value"}}

a formatter can display it as:

{
    "domain": "example.com",
    "registrar": "example",
    "userid": 123,
    "params": {
        "example": "value"
    }
}

This makes nested arrays and values considerably easier to inspect.

Using This Technique to Debug WHMCS Hooks

The same technique is useful when debugging an existing hook.

Simply add:

logActivity(json_encode($vars), 0);

inside the hook you are troubleshooting.

For example:

<?php

add_hook('SomeWHMCSHook', 1, function($vars) {

    logActivity(json_encode($vars), 0);

    // Rest of your hook code...

});

Trigger the event and then inspect the WHMCS Activity Log.

This allows you to verify not only which variables are available, but also the actual values WHMCS is passing at runtime.

This can be particularly useful when:

  • developing a new WHMCS hook;
  • debugging an existing hook;
  • checking whether a particular variable is available;
  • inspecting nested arrays;
  • verifying IDs and other values passed by WHMCS;
  • understanding the structure of data passed by a specific hook point.

Important: Remove Debug Logging Afterwards

This debugging code should normally be used temporarily.

Some WHMCS hooks can receive large arrays, and frequently executed hooks may generate a significant number of Activity Log entries.

Once you have collected the information you need, remove or comment out:

logActivity(json_encode($vars), 0);

This prevents unnecessary log growth.

There is another important consideration: hook variables may contain sensitive information.

Depending on the hook, $vars could potentially include customer information, authentication data, API-related information or other data that should not remain unnecessarily stored in a log.

For this reason, avoid leaving this type of debugging enabled on a production WHMCS installation.

A More Readable Alternative with print_r()

During development, you can also convert the array to a readable string using PHP’s print_r():

<?php

add_hook('PreRegistrarRegisterDomain', 1, function($vars) {

    logActivity(
        "PreRegistrarRegisterDomain variables:\n" .
        print_r($vars, true)
    );

});

This can sometimes be easier to read than raw JSON directly from the WHMCS Activity Log.

However, JSON has the advantage of being easy to copy into external JSON formatting and analysis tools.

Conclusion

When developing WHMCS hooks, you don’t necessarily need to guess which values are available inside $vars.

A simple temporary debug line:

logActivity(json_encode($vars), 0);

lets you inspect the complete set of variables actually passed to the hook at runtime.

It is a quick and effective technique for WHMCS hook development and debugging, particularly when working with complex or poorly documented hook data.

Just remember to disable the debug logging once you have finished troubleshooting.

Leave a Reply 0

Your email address will not be published. Required fields are marked *