LogoLogo
WordPress.orgExtensionsSupport
Version 9
Version 9
  • Notification – Custom Notifications and Alerts for WordPress
  • Updating to v9
  • Known issues
  • πŸ€Έβ€β™€οΈ User guide
    • Update broke my site
    • How Notification plugin works
    • Who can use this plugin
    • How to create Notifications
    • Troubleshooting
    • Advanced
      • How to escape { character
      • Background processing
      • How to setup different FROM address for different Notifications
      • Custom Post Type support
      • Disable upselling
      • How to send HTML Emails
  • πŸ”§Developer
    • General
      • Plugin loading chain
      • Runtime
      • Extension possibilities
      • Creating an extension
      • Customizations
      • Bundling Notification plugin
      • White label mode
    • Notifications
      • Suppressing the Notification
      • JSON synchronization
      • Programmatic Notifications
    • Triggers
      • List of all default Triggers
      • Custom Trigger
      • Enable support for non-public Custom Post Type
      • Adding Merge Tags to existing Triggers
    • Carriers
      • Custom Carrier
      • Adding custom fields to Carrier form
      • Suppressing the Carrier
    • Recipients
      • Custom Recipient
    • Snippets
      • General
        • Automatic Trigger testing
        • Allow other roles to edit Notifications
        • Programmatic Notification with manual Trigger
        • Background Processing filter
      • Triggers
        • Post
        • User
      • Integations
        • WP All Import
        • MemberPress
  • 🧩Extensions
    • Installation
    • Planned extensions
    • 3rd Party Extensions
    • Custom Fields
      • v2.2
      • v1.4
      • v1.3
    • Conditionals
    • Email Attachments
    • Push
    • Scheduled Triggers
    • Slack
    • Twilio
    • Webhooks
Powered by GitBook
On this page
  • Targeting all Post Type Triggers
  • Global Merge Tags

Was this helpful?

  1. Developer
  2. Triggers

Adding Merge Tags to existing Triggers

Sometimes it's needed to add your own Merge Tag to an 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:

use BracketSpace\Notification\Repository\MergeTag;
use BracketSpace\Notification\Repository\Trigger\Post\PostPublished;

// Hook into an action when Merge Tags are attached to Trigger.
add_action(
    'notification/trigger/merge_tags',
    function($trigger)
    {
        // Check if registered Trigger is the one you need.
        if (! $trigger instanceof PostPublished::class) {
            return;
        }

        // Pay attention to the Tag type you are defining.
        // If you want to output an HTML, use HtmlTag instead.
        $trigger->addMergeTag(
            new MergeTag\StringTag(
                [
                    'slug' => 'new_merge_tag',
                    'name' => __('New Merge Tag', 'textdomain'),
                    'resolver' => function($trigger) {
                        return get_post_meta($trigger->post->ID, '_my_meta_key', true);
                    },
                ]
            )
        );
    }
);

Since v9, dynamic property $trigger->{$post_type} has been replaced with a static prop $trigger->post.

Targeting all Post Type Triggers

You can target all the Post Type Triggers like this:

add_action(
    'notification/trigger/merge_tags',
    function($trigger)
    {
        if (! preg_match(
                '/post\/(.*)\/(updated|trashed|published|drafted|added|pending|scheduled)/',
                $trigger->getSlug()
        ) {
            return;
        }

        // Add your Tag.
    }
);

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:

use BracketSpace\Notification\Defaults\MergeTag\StringTag;
use BracketSpace\Notification\Register;

Register::globalMergeTag(new StringTag([
	'slug' => 'option_value',
	'name'=> __('Site option', 'textdomain'),
	'resolver' => function($trigger) {
		return get_option('your_option_name', 'Default');
	},
]));

Before using any property of the Trigger make sure it's available. Global Merge Tags are intended to be loosely connected with any Trigger.

PreviousEnable support for non-public Custom Post TypeNextCarriers

Last updated 6 months ago

Was this helpful?

πŸ”§