What is mean by WHMCS hook?

25 Sep

What is mean by WHMCS hook?

Standard way of modifying WHMCS features is using hook. WHMCS code is encrypted and we couldn’t change it. In addition to it, modifying core file is a bad idea. When we update WHMCS to the latest version all of our core file changes will be removed. So hook is a nice way to customize WHMCS features.

There are many hooks available in WHMCS. Available WHMCS hooks can be found in the link
https://developers.whmcs.com/hooks/hook-index/

Some of them can be used to run a piece of code just after an action is happened(ClientAdd executes just after a client is added to WHMCS). Some of them can be used used to run a piece of code just before an action(PreDeleteClient executes just before a client is deleted). There are hooks that can be used to run on certain pages(ClientAreaPageHome executes on the client area homepage).

Hooks can be include in the WHMCS in two ways.
1)We can create a php file with any name and can upload to the folder
/includes/hooks/.
2)We can create a file named hooks.php as a part of the addon module.

To get a better idea, let’s go through some examples now.
For now let’s just upload sample files to the folder /includes/hooks/.

Let’s add a hook ‘ClientAreaPage’ to a file named sample.php,
then let’s upload to the folder /includes/hooks/.
(see https://developers.whmcs.com/hooks-reference/client-area-interface/#clientareapage)

<?php
add_hook('ClientAreaPage', 1, function($vars) {
    echo "this is from clieant area hook";
    exit;
});

Then take any page in the client side, you will get a blank page with content
this is from clieant area hook. As we have exit in the code, nothing else will be displayed.

Now let’s modify the hook as shown below.

<?php
add_hook('ClientAreaPage', 1, function($vars) {
    echo "<pre>"; print_r($vars);
    exit;
});

Now the page will display an array.

To remove the hook, just delete the file from the folder /hooks/hook-index/.

Now let’s see another hook, ClientAreaPageCart.
Add the below code to a file named cart_hook.php and then upload to the folder
/includes/hooks/.

<?php
add_hook('ClientAreaPageCart', 1, function($vars) {
        echo "<pre>"; print_r($vars);
        exit;
});

This hook won’t run in all of the client are pages. But it will run in all of the
cart pages. For example take the url /cart.php then you will see an array printed in it.

Leave a Reply

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