Hooks are the functions that execute when associated events occur in Mumara. Learn how to create and execute your own codes in Mumara hooks.
Creating a Hook File
Hooks functions are written into PHP files and are placed inside /includes/hooks/ folder (or /Addons/your_addon/hooks.php). You'll find an example.php file placed inside /includes/hooks/ folder with example codes. The file name could be any name e.g. myfirsthook.php or aftercontactadd.php.
So let's create a sample hook for testing purpose
touch ~/includes/hooks/myfirsthook.php
Add a Sample Hook Function
Below is a sample hook function that will execute when a contact is added.
<?php
/**
* Register a hook
*
* AddContact: The hook function to listen when a contact is added
* 1: The priority to sort the multiple executions
* Function: Function to be called
*
*/
add_hook('AddContact',1, function($vars) {
echo "<pre>";
$data = "Hello World";
file_put_contents('storage/hello.txt', $data, FILE_APPEND | LOCK_EX);
});
The above hook function will create a hello.txt file in the /storage folder when a contact is added to any list. Most of the hooks return all values in $vars that can be useful in creating your codes that require the affected data and their values. You can use print_r($vars, true) to write all of the returned values to a file.
Recommendation! While using a named function, we suggest you double-check for any conflicts. The best way is to use named functions with a prefix.
Priority
Every hook needs a priority to be defined to arrange the sorting order for the executions when there are several hooks registered for the same event.
Note! The priority has to be an integer value.