Custom Trigger
Introduction
This feature enables developers to create their own triggers that respond to specific hooks in WordPress if the currently available triggers don't address your use case. By using the Custom trigger, you can enhance the functionality of your MailPoet site with custom automation. This document will guide you through the process of using this feature effectively.
Table of Contents
What is a Custom trigger?
How to Use a Custom Trigger
Hook Parameters
Using Custom Data
Example Usage
Conclusion
1. What is a Custom Trigger?
The Custom Trigger is a premium feature in MailPoet that allows you to create triggers that respond when specific hooks in WordPress are fired. Here are some key points to understand about Custom Triggers:
- Hook Activation: It gets triggered when a specific hook in WordPress is fired.
- Developer Control: It enables developers to create their own triggers, requiring PHP knowledge.
- Email Requirement: A valid email address is required as a parameter for the trigger.
2. How to Use a Custom Trigger
To use a Custom Trigger effectively, follow these steps:
- Select the Trigger
- Define the Hook: Specify the name of the hook to which the trigger should listen. This hook is the event that will activate your trigger.
- Create a Plugin: Write a small plugin that fires the hook specified in step 2.2. This plugin will initiate your trigger when the hook is activated.
3. Hook Parameters
The hook you define in your Custom Trigger must send specific parameters. Here are the requirements:
- Parameter 1: Must be a valid email address. This email address is used by the automation to look up the subscriber or create a new subscriber.
- Parameter 2 (Optional): You can use a second parameter to send custom data. This custom data must be an array following the structure below:
$customData = [ 'custom-value-1' => [ 'value' => 'your-value', ], 'custom-value-2' => [ 'value' => 1, ], ];
You have the flexibility to define the keys of the value arrays as you see fit. However, each custom value array must contain the value property, and its value must be a scalar value.
4. Using Custom Data
The custom data you send via the hook parameters can be utilized in the Custom Action. This allows you to leverage this data in your automation's logic.
5. Example Usage
Here's an example of how you can use the Custom Trigger:
Scenario: Sending a "Welcome Back" Email to Users upon Login.
<?php /** * Plugin Name: Welcome back automation */ add_action( 'wp_login', function($username, $user) { do_action( 'welcome_back_automation', $user->user_email ); }, 10, 2 );
You see how we use the hook welcome_back_automation. In our automation, we need to set this name now in the trigger settings:
In a real-world application, you can extend this logic further. For instance, you may want to send the "Welcome Back" email only when the user logs in after a specific duration, such as more than a week.
6. Conclusion
With some knowledge of PHP and WordPress development, the Custom Trigger can empower you to create powerful automations for your WordPress site. Feel free to explore various possibilities and enhance your site's functionality with custom triggers tailored to your needs.