Search code examples
phphookwhmcs

WHMCS Action Hooks Not Firing


I have been pounding my head against the desk for a week now and have finally decided it's time to ask for help.

I am trying to create action hooks for WHMCS for when a client performs a certain action. For example, i'm just trying to get it to return the variables it grabs. Essentially all I need is the USER ID, but for testing purposes I asked it to return several variables.

Below is an example of my code:

/*
 * Define Hook Functions 
*/
function hook_changedpassword($vars) {
    $userid = $vars['userid'];
    $firstname = $vars['firstname'];
    $lastname = $vars['lastname'];
    $email = $vars['email'];
    $password = $vars['password'];

    return print_r($vars);
}

function hook_clientmerged($vars) {
    $userid = $vars['userid'];
    $firstname = $vars['firstname'];
    $lastname = $vars['lastname'];
    $email = $vars['email'];
    $password = $vars['password'];

    return print_r($vars);  
}

function hook_clientupdated($vars) {
    $userid = $vars['userid'];
    $firstname = $vars['firstname'];
    $lastname = $vars['lastname'];
    $email = $vars['email'];
    $password = $vars['password'];

    return print_r($vars);  
}

function hook_emailverification($vars) {
    $userid = $vars['userid'];
    $firstname = $vars['firstname'];
    $lastname = $vars['lastname'];
    $email = $vars['email'];
    $password = $vars['password'];

    return print_r($vars);  
}

/*
 * Start Add Hook Functions 
*/

// when client changes portal password
add_hook('ClientChangePassword', 1, "hook_changedpassword");

// when client accounts are merged.
add_hook('AfterClientMerge', 1, "hook_clientmerged");

// when client updates client details
add_hook('AfterClientMerge', 1, "hook_clientupdated");

// when client verifies email address
add_hook('ClientEmailVerificationComplete', 1, "hook_emailverification");

It's not associated with any module or addon, and I don't know if that's the problem, but the Hooks Wiki makes it appear that regardless, these will be processed when these actions are taken.

Any help would be greatly appreciated.


Solution

  • If you check the Hook documentation, some hooks doesn't support returning any values, for example, AfterClientMerge, No response supported.

    Probably your hooks are firing, but they're doing nothing. WHMCS somehow suppressing browser output in some hooks, even echo will not show an output.

    Such hooks expected to perform an action, like updating DB table or sending an email, etc.

    For debugging, use the following statement to output variables to a file instead outputting them to the browser:

    error_log(print_r($vars, true), 3, __DIR__.'/file.log');
    

    Function becomes:

    function hook_clientmerged($vars) {
        $userid = $vars['userid'];
        $firstname = $vars['firstname'];
        $lastname = $vars['lastname'];
        $email = $vars['email'];
        $password = $vars['password'];
    
        error_log(print_r($vars, true), 3, __DIR__.'/file.log');  
    }
    

    Edit: To insert data to database

    File: example_hook.php

    <?php
    use WHMCS\Database\Capsule as DB;
    
    function hook_clientmerged($vars) {
        $userid = $vars['userid'];
        $firstname = $vars['firstname'];
        $lastname = $vars['lastname'];
        $email = $vars['email'];
        $password = $vars['password'];
    
        //Insert vars in the database
        DB::table('table_name')->insert(
            ['field1' => $userid, 'field2' => $firstname]
        ); 
    }