Here’s a quick example of how you can use WHMCS hooks. Let’s say you want to log a message to the WHMCS activity log every time a client logs in.
Step 1: Create the Hook File
Create a PHP file within your WHMCS directory under /includes/hooks/
. You can name it something like client_login_log.php
.
Step 2: Write the Hook Code
Inside the client_login_log.php
file, write the following code:
php
<?php add_hook('ClientLogin', 1, function($vars) { logActivity("Client " . $vars['userid'] . " logged in from IP: " . $vars['ip']); }); ?>
Step 3: Upload and Test
Upload the file to your WHMCS server, and the next time a client logs in, a message will be added to the WHMCS activity log, noting the user ID and IP address.
That’s it! This simple example illustrates how to use a WHMCS hook to extend functionality. By using different hook points and custom code, you can tailor WHMCS to fit your unique requirements. Make sure to consult the WHMCS documentation for details on the many available hooks and their parameters. Now lets look at some examples:
WHMCS is a powerful billing and support solution. One of its strengths is the ability to use hooks to extend its functionality. In this blog post, we’ll explore some of the most useful hooks, providing multiple examples for each.
1. AfterCronJob: Execute Actions After the Daily Cron Job
Example 1: Log Cron Job Completion
Log when the daily cron job is completed:
add_hook('AfterCronJob', 1, function() { logActivity("Daily Cron Job Completed"); });
Example 2: Send Notification
Notify admins after the daily cron job:
add_hook('AfterCronJob', 1, function() { sendAdminNotification("Daily Cron Job Completed"); });
2. ClientLogin: Perform Actions When a Client Logs In
Example 1: Log Client Login
Keep a log of client logins:
add_hook('ClientLogin', 1, function($vars) { logActivity("Client " . $vars['userid'] . " logged in"); });
Example 2: Redirect Based on Custom Field
Redirect clients based on a custom field value:
add_hook('ClientLogin', 1, function($vars) { $customField = get_query_val('tblcustomfieldsvalues', 'value', ['fieldid' => 1, 'relid' => $vars['userid']]); if ($customField == 'Special') { header('Location: special_dashboard.php'); exit; } });
3. ClientAreaPage: Modify or Add Data to Client Area Pages
Example 1: Add Global Announcement
Display a global announcement across client area pages:
add_hook('ClientAreaPage', 1, function($vars) { return ['global_announcement' => 'System will be down for maintenance on Sunday']; });
Example 2: Inject Custom JavaScript
Add custom JavaScript to client area pages:
add_hook('ClientAreaPage', 1, function($vars) { return ['custom_js' => '<script src="custom.js"></script>']; });
4. PreModuleCreate: Execute Actions Before a Module Creates an Account
Example 1: Validate Custom Fields
Ensure custom fields meet specific criteria:
add_hook('PreModuleCreate', 1, function($vars) { if ($vars['customfields']['Field Name'] != 'Expected Value') { throw new Exception("Custom Field validation failed."); } });
Example 2: Check for Duplicate Accounts
Prevent duplicate accounts:
add_hook('PreModuleCreate', 1, function($vars) { // Check for duplicate accounts and take necessary actions });
5. AfterModuleTerminate: Execute Actions After an Account Has Been Terminated
Example 1: Notify Admin of Termination
Inform the admin after an account termination:
add_hook('AfterModuleTerminate', 1, function($vars) { sendAdminNotification("Account " . $vars['serviceid'] . " terminated"); });
Example 2: Archive User Data
Archive data related to a terminated account:
add_hook('AfterModuleTerminate', 1, function($vars) { // Code to archive user data related to the terminated account });
6. TicketOpen: Perform Actions When a Support Ticket Is Opened
Example 1: Assign Ticket to Specific Department
Assign tickets to a specific admin based on the department:
add_hook('TicketOpen', 1, function($vars) { if ($vars['deptid'] == 1) { // Code to assign the ticket to a specific admin } });
Example 2: Send a Custom Auto-Reply
Send a custom auto-reply when a ticket is opened:
add_hook('TicketOpen', 1, function($vars) { sendMessage('Custom Auto Reply', $vars['ticketid']); });
7. InvoicePaid: Trigger Actions When an Invoice Is Paid
Example 1: Send a Custom Thank-You Email
Send a thank-you email when an invoice is paid:
add_hook('InvoicePaid', 1, function($vars) { sendMessage('Thank You Email', $vars['invoiceid']); });
Example 2: Activate a Related Service
Automatically activate a related service:
add_hook('InvoicePaid', 1, function($vars) { // Code to activate a related service when an invoice is paid });
8. EmailPreSend: Modify Email Content Before Sending
Example 1: Add a Disclaimer
Append a disclaimer to all outgoing emails:
add_hook('EmailPreSend', 1, function($vars) { $vars['messagetext'] .= "\n\nDisclaimer: ..."; return $vars; });
Example 2: Replace Custom Placeholders
Replace placeholders in email content:
add_hook('EmailPreSend', 1, function($vars) { $vars['messagetext'] = str_replace('[placeholder]', 'Replacement Text', $vars['messagetext']); return $vars; });
9. AdminLogin: Execute Actions When an Admin Logs In
Example 1: Log Admin Login Attempts
Log all admin login attempts:
add_hook('AdminLogin', 1, function($vars) { logActivity("Admin " . $vars['username'] . " logged in from " . $vars['ip']); });
Example 2: Send a Notification for Specific Admin Logins
Notify when a specific admin logs in:
add_hook('AdminLogin', 1, function($vars) { if ($vars['username'] == 'superadmin') { sendAdminNotification("Super Admin Logged In"); } });
10. AddonActivation: Perform Actions When an Addon Is Activated
Example 1: Perform Setup Task for Specific Addon
Setup tasks for a specific addon:
add_hook('AddonActivation', 1, function($vars) { if ($vars['addonid'] == '123') { // Code to perform setup tasks for this specific addon } });
Example 2: Notify Admin of Addon Activation
Notify admin when an addon is activated:
add_hook('AddonActivation', 1, function($vars) { sendAdminNotification("Addon " . $vars['addonid'] . " activated"); });
Conclusion
WHMCS hooks provide powerful ways to extend the functionality of WHMCS. These examples demonstrate how to utilise them in various scenarios. Always consult the official WHMCS documentation to ensure compatibility with your specific version and setup. Happy coding!