mikelegg Posted March 12, 2012 Share Posted March 12, 2012 I'm trying to create my first action hook using the example provided with WHMCS, but when I directly run the PHP script from a browser, I get ... Fatal error: Call to undefined function add_hook() I assume that's because the add_hook() function is hidden away in the encrypted WHMCS PHP code and so the files in the /includes/hooks folder only work when executed by WHMCS. My script uses the InvoiceCreationPreEmail hook, but it doesn't run when I manually create an invoice or when I place an order which includes an invoice. So how can I test my PHP hook scripts? What confuses me even further is that I have another action hook which I downloaded from this forum which works fine, but doesn't even use the "add_hook()" function. 0 Quote Link to comment Share on other sites More sharing options...
laszlof Posted March 12, 2012 Share Posted March 12, 2012 Thats correct, you cannot call hook functions directory. The best way to troubleshoot them is to var_dump/print/print_r/echo various variables and output throughout the process. This can be difficult to track when you're using hooks that run in the background, like the one you're attempting to use. The solution for this is to use the output buffer to capture all of the output to a variable, then either write it to a file or the logs. One note about using the output buffer, if you have an error in your script that causes it to exit prematurely before the ob_get_clean() call, it will not output properly. Heres a quick example of using the output buffer. <?php function mytesthook($vars) { ob_start(); echo "FUNCTION STARTED\n"; var_dump($vars); echo "THIS IS A TEST"; $output = ob_get_clean(); logActivity($output); } add_hook('InvoiceCreationPreEmail', 999, 'mytesthook'); ?> This will output the results into the WHMCS activity log. Hope this helps. 0 Quote Link to comment Share on other sites More sharing options...
mikelegg Posted March 13, 2012 Author Share Posted March 13, 2012 Thanks Frank - that's very helpful advice 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.