Adding Merge Tags to existing Triggers
Sometimes it's needed to add your own Merge Tag to already defined Trigger. In example you can add a custom meta Merge Tag to default Post Published Trigger.
To do this simply hook into the action and add your Merge Tag:
1
// Hook into an action when Merge Tags are attached to Trigger.
2
add_action( 'notification/trigger/merge_tags', function( $trigger ) {
3
​
4
// Check if registered Trigger is the one you need.
5
if ( 'post/post/updated' !== $trigger->get_slug() ) {
6
return;
7
}
8
​
9
// Pay attention to the Tag type you are defining.
10
// If you want to output an HTML, use HtmlTag instead.
11
$trigger->add_merge_tag( new BracketSpace\Notification\Defaults\MergeTag\StringTag( [
12
'slug' => 'new_merge_tag',
13
'name' => __( 'New Merge Tag', 'textdomain' ),
14
'resolver' => function( $trigger ) {
15
return get_post_meta( $trigger->{ $trigger->get_post_type() }->ID, '_my_meta_key', true );
16
},
17
] ) );
18
​
19
} );
Copied!

Targeting all Post Type Triggers

You can target all the Post Type Triggers like this:
1
add_action( 'notification/trigger/merge_tags', function( $trigger ) {
2
​
3
if ( ! preg_match( '/post\/(.*)\/(updated|trashed|published|drafted|added|pending|scheduled)/', $trigger->get_slug() )
4
return;
5
}
6
​
7
// Add your Tag.
8
​
9
} );
Copied!

Global Merge Tags

The Notification plugin also supports global Merge Tags which are added to all Triggers. You can use this nifty function to create one:
1
use BracketSpace\Notification\Defaults\MergeTag\StringTag;
2
use BracketSpace\Notification\Register;
3
​
4
Register::global_merge_tag( new StringTag( [
5
'slug' => 'option_value',
6
'name' => __( 'Site option', 'textdomain' ),
7
'resolver' => function( $trigger ) {
8
return get_option( 'your_option_name', 'Default' );
9
},
10
] ) );
Copied!
Before using any property of the Trigger make sure it's avaible. Global Merge Tags are intended to be loosly connected with any Trigger.
Last modified 4d ago