DEV Community

Shahid Malla
Shahid Malla

Posted on

10 WHMCS Hooks Every Hosting Company Should Use

Hooks are the easiest way to customize WHMCS without touching core files. Drop a PHP file in /includes/hooks/ and WHMCS runs your code when an event happens.

Here are 10 hooks I add for almost every hosting client.

1. Notify your team when an invoice is paid

<?php
add_hook('InvoicePaid', 1, function ($vars) {
    $msg = "Invoice #{$vars['invoiceid']} paid";
    $ch = curl_init('https://hooks.slack.com/services/XXX');
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['text' => $msg]));
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    curl_close($ch);
});
Enter fullscreen mode Exit fullscreen mode

2. Block disposable email signups

add_hook('ClientDetailsValidation', 1, function ($vars) {
    $blocked = ['mailinator.com', 'tempmail.com', '10minutemail.com'];
    $domain = strtolower(substr(strrchr($vars['email'], '@'), 1));
    if (in_array($domain, $blocked)) {
        return ['Please use a real email address.'];
    }
});
Enter fullscreen mode Exit fullscreen mode

3. Add a custom message to the client area

add_hook('ClientAreaHomepagePanels', 1, function ($panels) {
    $panels->addChild('notice', [
        'label' => 'Scheduled Maintenance',
        'icon' => 'fa-wrench',
        'bodyHtml' => '<p>Server maintenance on Sunday 2 AM UTC.</p>',
        'order' => 1,
    ]);
});
Enter fullscreen mode Exit fullscreen mode

4. Tag high value clients automatically

use WHMCS\Database\Capsule;

add_hook('InvoicePaid', 1, function ($vars) {
    $invoice = Capsule::table('tblinvoices')->find($vars['invoiceid']);
    if ($invoice && $invoice->total >= 500) {
        Capsule::table('tblclients')
            ->where('id', $invoice->userid)
            ->update(['groupid' => 2]); // VIP group
    }
});
Enter fullscreen mode Exit fullscreen mode

5. Sync new clients to your CRM or mailing list

Use ClientAdd to push the new client's name and email to your CRM API.

6. Open a ticket when provisioning fails

add_hook('AfterModuleCreateFailed', 1, function ($vars) {
    localAPI('OpenTicket', [
        'deptid' => 1,
        'subject' => 'Provisioning failed for service #' . $vars['params']['serviceid'],
        'message' => $vars['failureResponseMessage'],
        'clientid' => $vars['params']['userid'],
        'admin' => true,
    ]);
});
Enter fullscreen mode Exit fullscreen mode

Now no failed order goes unnoticed.

7. Add extra variables to email templates

add_hook('EmailPreSend', 1, function ($vars) {
    return ['support_hours' => 'Mon to Sat, 9 AM to 9 PM IST'];
});
Enter fullscreen mode Exit fullscreen mode

Use {$support_hours} in any template.

8. Redirect after logout

add_hook('ClientLogout', 1, function () {
    header('Location: https://yourhost.com/');
    exit;
});
Enter fullscreen mode Exit fullscreen mode

9. Add tracking scripts to the client area footer

add_hook('ClientAreaFooterOutput', 1, function ($vars) {
    return '<script>/* analytics code */</script>';
});
Enter fullscreen mode Exit fullscreen mode

10. Daily cleanup or reports with the cron

add_hook('DailyCronJob', 1, function () {
    // send yourself a daily revenue summary, clean old logs, etc.
});
Enter fullscreen mode Exit fullscreen mode

Best practices

  • One hook per file with a clear name like slack_invoice_paid.php.
  • Wrap risky code in try/catch so one error doesn't break checkout.
  • Use Capsule for database queries, never raw SQL with user input.
  • Test on staging first. A broken hook can take down your whole client area.

Wrapping up

Hooks give you 80 percent of the customization most hosts need with very little code. If you need something more complex, that's when an addon module makes sense.

I'm Shahid Malla, a WHMCS developer from Kashmir. Share your favorite hook in the comments.

Top comments (0)