Jump to content

Call to undefined function add_hook()


mikelegg

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated