Difference between revisions of "Hooks"

From WHMCS Documentation

Line 1: Line 1:
Action hooks allow you to add your own code that will run when specific actions occur in WHMCS.  For example, this can be used for things like performing other actions when an order is placed, sending an SMS message when a support ticket is opened, updating a users details in another application when a change is made in WHMCS, and many more...
+
==What is a Hook?==
  
==How It Works==
+
Action Hooks allow you to add your own code to WHMCS that runs when specific events or actions occur inside the system.  WHMCS has over 100 different hook points that you can tie custom code into, and more are being added with each new release.
  
Hooks work by creating a file in the includes/hooks folder.  Within the file, you need to declare a function that performs the action you want to run.  That function will be passed variables from WHMCS, and the values you get depend on the hook being called.  All hooks and the variables they will be passed are listed below.  After defining your function, you then add the hook to WHMCS which is done by calling the add_hook function:
+
The possibilities with hooks are endless, but some common examples of what you might use them for are:
  
  function create_forum_account($vars) {
+
*Registering users in another system when they signup via WHMCS
     print_r($vars);
+
*Keeping user details in sync with third party applications
 +
*Performing additional custom actions when an order is placed in WHMCS
 +
*Sending notifications when certain events occur
 +
 
 +
{{Hook_Categories}}
 +
 
 +
==Creating a Hook==
 +
 
 +
Hooks work by defining a function, and then tieing that function into a hook point. Most custom hook functions are created as a standalone hook, and thus reside in the /includes/hooks/ folder. But hooks can also be part of modules (more details later on this).
 +
 
 +
Custom hook functions are usually passed variables when they are called, with the variables depending on the action the hook point relates to. Some hook points will also accept a return from your custom hook function, and that allows you to influence what the system does next from your custom code.
 +
 
 +
==Sample Code==
 +
 
 +
The best way to see how a hook works is with an example. So here's a sample hook function integrated into the Client Signup hook event:
 +
 
 +
<?php
 +
 +
  function hook_create_forum_account($vars) {
 +
 +
    $firstname = $vars['firstname'];
 +
    $lastname = $vars['lastname'];
 +
     $email = $vars['email'];
 +
 +
    # Run code to create remote forum account here...
 +
 
  }
 
  }
  add_hook("ClientAdd",1,"create_forum_account");
+
 +
  add_hook("ClientAdd",1,"hook_create_forum_account");
 +
 +
?>
 +
 
 +
==The Add Hook Function==
 +
 
 +
This is the key function for hooks, and is how you define to WHMCS at what point your custom hook function should be called.
  
*The first part defines the hook point.  This must be one of the hook points in the categories below: ClientAdd, AddInvoicePayment, TicketOpen, etc...
+
*The first part defines the hook point.  This must be one of the hook points WHMCS has implemented (listed in the pages below)
*The number defines the priority so if you have more than 1 hook for the ClientAdd function, the order in which the hooks should run
+
*The number defines the priority of the hook, so for example if you have more than 1 hook for the ClientAdd function, the order in which the hooks should run
*The third argument tells WHMCS which function to run at this hook point and so this is the name of the function which you have written in your hook file
+
*The third argument tells WHMCS which function to call from this hook point and so this is where you put the name of the custom hook file you create
  
 
A commented out example hook file is included in WHMCS which you can use as a template. You'll find that in the folder /includes/hooks/
 
A commented out example hook file is included in WHMCS which you can use as a template. You'll find that in the folder /includes/hooks/
 +
 +
==Module Hooks==
 +
 +
As of WHMCS V4.4, addon modules support hooks being defined that relate to a module within the module directory in /modules/addons/
 +
 +
And as of WHMCS V5.0, both product/server/provisioning modules and domain registrar modules also support hooks being defined within the relevant modules directories.
 +
 +
To define hooks as part of a module, simple create a file named "hooks.php" within the module folder, and include all module specific hook related code within that file.
 +
 +
''Note to Developers:'' Hook files in module folders are only called for active modules. And active modules that contain hooks are cached. So if you add a hooks file to a module retrospectively, once the module is already active, then before the system will detect and start running that module, you must edit and resave either the addon module configuration, product configuration or domain registrar configuration for the respective module for it to be detected.
  
 
==Hook Points==
 
==Hook Points==
  
Hooks are divided up into categories to help make it easier to find exactly what you're looking for. So simply choose a category from below to see the hooks which exist for that, details of when they run and what variables they are passed.
+
We've divided the hooks up into categories to help make the list more managable. So simply choose an option from below to see the hooks which exist for that category.
  
 
{{Hook_Categories}}
 
{{Hook_Categories}}

Revision as of 01:08, 9 October 2011

What is a Hook?

Action Hooks allow you to add your own code to WHMCS that runs when specific events or actions occur inside the system. WHMCS has over 100 different hook points that you can tie custom code into, and more are being added with each new release.

The possibilities with hooks are endless, but some common examples of what you might use them for are:

  • Registering users in another system when they signup via WHMCS
  • Keeping user details in sync with third party applications
  • Performing additional custom actions when an order is placed in WHMCS
  • Sending notifications when certain events occur

Creating a Hook

Hooks work by defining a function, and then tieing that function into a hook point. Most custom hook functions are created as a standalone hook, and thus reside in the /includes/hooks/ folder. But hooks can also be part of modules (more details later on this).

Custom hook functions are usually passed variables when they are called, with the variables depending on the action the hook point relates to. Some hook points will also accept a return from your custom hook function, and that allows you to influence what the system does next from your custom code.

Sample Code

The best way to see how a hook works is with an example. So here's a sample hook function integrated into the Client Signup hook event:

<?php

function hook_create_forum_account($vars) {

    $firstname = $vars['firstname'];
    $lastname = $vars['lastname'];
    $email = $vars['email'];

    # Run code to create remote forum account here...

}

add_hook("ClientAdd",1,"hook_create_forum_account");

?>

The Add Hook Function

This is the key function for hooks, and is how you define to WHMCS at what point your custom hook function should be called.

  • The first part defines the hook point. This must be one of the hook points WHMCS has implemented (listed in the pages below)
  • The number defines the priority of the hook, so for example if you have more than 1 hook for the ClientAdd function, the order in which the hooks should run
  • The third argument tells WHMCS which function to call from this hook point and so this is where you put the name of the custom hook file you create

A commented out example hook file is included in WHMCS which you can use as a template. You'll find that in the folder /includes/hooks/

Module Hooks

As of WHMCS V4.4, addon modules support hooks being defined that relate to a module within the module directory in /modules/addons/

And as of WHMCS V5.0, both product/server/provisioning modules and domain registrar modules also support hooks being defined within the relevant modules directories.

To define hooks as part of a module, simple create a file named "hooks.php" within the module folder, and include all module specific hook related code within that file.

Note to Developers: Hook files in module folders are only called for active modules. And active modules that contain hooks are cached. So if you add a hooks file to a module retrospectively, once the module is already active, then before the system will detect and start running that module, you must edit and resave either the addon module configuration, product configuration or domain registrar configuration for the respective module for it to be detected.

Hook Points

We've divided the hooks up into categories to help make the list more managable. So simply choose an option from below to see the hooks which exist for that category.