Hooking functions Hooking functions allows you to replace a function value in an accessible environment by a function of your own , calling the original function before or after execution of new code . Hooking enables addons to modify and / or extend the behavior of the default UI and API . Depending on how the behavior of the original function is modified , one or more of three hook types may be used : Functions as values . Functions are first - class values in Lua : they may be assigned to variables , used as table keys , passed as arguments and returned from functions just like any other value type . Because API and public interface functions reside in the global namespace , and addons rarely use local aliases , it is possible to alter the value of a variable storing a function - - and have existing code call the new function . This makes it possible to modify function behavior by hooking . The traditional function declaration , shown below , is merely syntactic sugar for the assignment of a function value to the variable specified by the function name . Thus , the following two lines are identical : Because most addons ( and the default UI code ) call most functions without using a local reference , it is possible to retrieve and change the value stored in a global variable , and get the addons to call your new function rather than the old global one , thereby enabling you to override its behavior . For example : In the above example , the original value of error was discarded ; in hooking , the original value is typically called by the new function . Hooking a function . To illustrate the concept , let ' s create a pre - hook . Pre - hooks are commonly used to override user interface functions that perform an ( unprotected ) action , potentially adding behavior to existing UI . In this example , hook ( a function called when the user clicks on a chat link ) to display a faux tooltip for the , an item currently not in the game . Explained in detail , the preceding code does : The example used the vararg expression to make sure that , should the API change , the hook will still pass exactly the arguments it was given ( no more , no less ) to the original function . An additional sanity check on the type of the link argument in 3 prevents a potential error : if the function was not passed a string as expected ( for example , by another addon ) , we do not want to block the default behavior . Post - hooking . Post - hooks are used to act on an API function call performed by another addon / the user / the default UI , and respond to the performed action when there ' s no reliable event - based notification . For instance , companions and GetCursorInfo do not function well as of Patch 3 . 0 . 9 , so it may be desirable to obtain the information about the last companion picked up on the cursor from the PickupCompanion call . So we post - hook : The syntax here is a bit convoluted , defining two functions to accomplish create the hook ; this construction is used to make sure that all of the arguments ( even if a future patch adds / remove additional arguments ) are passed to the original function , and all of its return values are returned by the hook . Step - by - step description of this syntax : Hooking secure functions . Secure functions cannot be hooked directly - - as that would taint the variable holding the function , and , as a result , the execution path to the secure function , causing it to fail . Instead , the hooksecurefunc function can be used to apply a post - hook . Unlike the variant above , the values returned by the original function are not supplied . Let ' s apply the same hook to PickupCompanion securely : Hooking widget handlers . You can also hook widget handles ( OnClick / OnShow / On . . . scripts attached to widgets ) . There are two general mechanics that you could use : Hooking object methods . Functions called using the object calling syntax , codice _ 1 , can also be hooked ; one simply has to remember that the construct is merely syntactic sugar for codice _ 2 . To hook a function that was originally declared as : We store the original function and replace it with a hook : The example hook above will only allow the original function to be called if the first argument is a strictly positive number . Removing existing hooks . Removing hooks is generally problematic : there ' s no way to remove a hooksecurefunc - based post - hook , and insecure hooks may only be removed in some situations . The difficulty arises from the fact that multiple addons may want to hook the same function - - and if any addon but the top - most in the hooking chain wants to unhook , there ' s no way to tell the hook on top of your function to call the function below . It is therefore recommended to simply make your existing hook pass arguments and return values directly through if you don ' t want to deal with the data . For example : Libraries may contain functions that allow you to " unhook " your method - - those operate primarily by moving the ignore switch further up the execution tree : if you remove your hook , it won ' t get called , but the library ' s hook function stays in place .