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 just after the Trigger is registered.
2
add_action( 'notification/trigger/registered', function( $trigger ) {
3
4
// Check if registered Trigger is the one you need.
5
if ( 'wordpress/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->post->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/registered', function( $trigger ) {
2
3
if ( ! preg_match( '/wordpress\/(.*)\/(updated|trashed|published|drafted|added|pending)/', $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
notification_add_global_merge_tag( new BracketSpace\Notification\Defaults\MergeTag\StringTag( [
2
'slug' => 'option_value',
3
'name' => __( 'Site title', 'textdomain' ),
4
'resolver' => function( $trigger ) {
5
return get_option( 'your_option_name', 'Default' );
6
},
7
] ) );
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 2yr ago