Custom Action
The Custom action feature allows you to fire a hook of your choice, enabling developers to implement specific logic within a plugin. This document will guide you through the process of using Custom actions effectively. If you're not already familiar with the Custom trigger, please review our documentation on it, as the Custom action is essentially the opposite, firing a hook instead of listening to one.
Table of Contents
What is a Custom Action?
Using Custom Actions
Example Usage
Custom Data
Conclusion
1. What is a Custom Action?
The Custom action is a premium feature in MailPoet Automations that allows you to fire a hook of your choice. Key points to understand about Custom actions:
- Hook Activation: It fires a hook that you can specify.
- Developer Control: Developers can create a plugin that listens to these hooks and implements specific logic.
- PHP and WordPress Knowledge: To effectively utilize Custom actions, knowledge of PHP and WordPress development is required.
2. Using Custom Actions
To use a Custom action effectively, follow these steps:
- Setup the action in your automation: Specify the hook you want to fire using the Custom action. This hook will serve as the event that triggers your custom logic.
- Create a Plugin: Develop a plugin that listens to the hook specified in step 2.1. This plugin should implement the desired logic that responds to the fired hook.
3. Example Usage
Continuing from the example in the Custom trigger documentation, let's extend the "Welcome Back" automation with a Custom action. In this scenario, we want to block the "welcome back" email for seven days before sending it again. Here's how you can achieve this:
As you can see, after sending the email we wait now seven days and after this we will fire the "welcome_back_end" hook. The code of our plugin is now the following:
<?php /** * Plugin Name: Welcome back automation */ add_action( 'wp_login', function($username, $user) { if (get_user_meta($user->ID, 'welcome_back_automation_running', true)) { return; } do_action( 'welcome_back_automation', $user->user_email ); add_user_meta($user->ID, 'welcome_back_automation_running', 1, true); }, 10, 2 ); add_action( "welcome_back_automation_end", function($subscriber_email) { $user = get_user_by('email', $subscriber_email); delete_user_meta($user->ID, 'welcome_back_automation_running', 1); } );
After the user logs in, we first check if the user meta "welcome_back_automation_running" exists. In this case, we do not continue. If it does not exist we start the automation by firing "welcome_back_automation" and we set the "welcome_back_automation_running" value. So the next time the user logs in, the automation would not start.
Additionally now, we do also listen to "welcome_back_automation_end", which fires after the delay of seven days. In this callback, we delete the user meta "welcome_back_automation_running". So after seven days, the user could enter the automation again.
4. Custom Data
If you have sent custom data through your "welcome_back_automation" as a second parameter when using the Custom trigger, you can receive this parameter as a second parameter in your Custom action. This allows you to access and use the custom data within your custom logic.
We extend our example just with some dummy code to show how this could look like:
<?php /** * Plugin Name: Welcome back automation */ add_action( 'wp_login', function($username, $user) { if (get_user_meta($user->ID, 'welcome_back_automation_running', true)) { return; } $custom_data = [ 'foo' => [ 'value' => 'bar', ], ]; do_action( 'welcome_back_automation', $user->user_email, $custom_data ); add_user_meta($user->ID, 'welcome_back_automation_running', 1, true); }, 10, 2 ); add_action( "welcome_back_automation_end", function($subscriber_email, $custom_data) { if (!isset($custom_data['foo']) || !$custom_data['foo'] !== 'bar') { return; } $user = get_user_by('email', $subscriber_email); delete_user_meta($user->ID, 'welcome_back_automation_running', 1); }, 10, 2 );
5. Conclusion
With the Custom action feature in MailPoet, you have the power to fire hooks and implement custom logic within your plugins. This flexibility allows you to create sophisticated and tailored automations for your WordPress site, enhancing its functionality to meet your specific needs. Explore the possibilities and make the most of Custom actions in your development endeavors.