DevOps is crucial to your cloud-native

Table of contents


Hooks, actions, filters - custom MailWizz extension

MailWizz is modular and allows you to interact with pieces of code within. It does this by use of hooks. MailWizz offers two hooks called ‘Action’ and ‘Filters’. You can write a custom function that can use both of these. In addition, a custom function, Callback, can register with a hook for a specific Action or Filter. Several actions and filters are available and extensive documents that detail their inner workings.

Actions allow you to add or change how MailWizz functions. The function you write can run at a specific point during the normal functioning of the software and perform a task that executes your custom logic. The task can be anything from showing a particular text to the user or inserting something into the database. Here is an example of adding an action passing at least two parameters - string $tag and callback $callback. The following code will add an action to the daily cron command to execute the _runBackup method.

Yii::app()->hooks->addAction('console_command_daily', array($this, '_runBackup'), 1000);

You can use the following code to run the action. For the ‘Action’ code above, the following will trigger the Callback execution:

Yii::app()->hooks->doAction('console_command_daily', $this);

Filters enables you to change data during the execution of normal MailWizz functions. The custom functions for a Filter will accept a variable, modify it, and return it. They work in isolation and have no side effects that affect the global variables and outputs. For example, the following code calls the filter when you request the Callback function.

Yii::app()->hooks->addFilter($tag, $callback, $priority = 10)

Here is another example of adding a navigation menu item that will execute the method of the class where the filter was added _registerBackendMenuItem

// add the menu item
Yii::app()->hooks->addFilter('backend_left_navigation_menu_items', array($this, '_registerBackendMenuItem'))

You can apply the filter using the $tag, $arg parameters, the filter name, and the variable of that filter. For applying that navigation menu item, you would do something like $menuItems = (array)Yii::app()->hooks->applyFilters('backend_left_navigation_menu_items', $menuItems);

We build custom extensions specifically for MailWizz as part of our hosting services. Please feel free to get in touch to enquire about implementing your business logic right into the software. We can learn about your requirements and deliver within your budget and timeframe.