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
  • Trigger
  • Notification
  • Action

Was this helpful?

  1. Developer
  2. Snippets
  3. General

Programmatic Notification with manual Trigger

Send the notification from the code, nothing needs to be configured in the Dashboard.

To make it work you'll need:

  • Custom Manual Trigger

  • Notification definition

  • Action to trigger the Notification

Trigger

Let's define a simple Trigger without any Merge Tags.

use BracketSpace\Notification\Repository\Trigger\BaseTrigger;

/**
 * Manual trigger class
 * 
 * This needs to be defined at `notification/init` action.
 */
class ManualTrigger extends BaseTrigger
{
    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct(
            'support/manual_trigger',
            'Manual Trigger'
        );

        $this->addAction('notification_manual_trigger_348m7t5', 10);

        $this->setGroup('Support');

        $this->setDescription('Triggered with notification_manual_trigger_348m7t5 action.');
    }

    /**
     * Action callback
     *
     * @return void
     */
    public function context()
    {
        // This trigger should always process.
    }

    /**
     * Merge Tags
     *
     * @return void
     */
    public function merge_tags()
    {
        // This trigger doesn't include any Merge Tags.
    }
}

Notification

The notification is just a function call with a configuration array. We'll register the Notification along with the Trigger.

use BracketSpace\Notification\Register;

add_action(
    'notification/init',
    function() {
        $manual_trigger = new ManualTrigger;

        Register::trigger($manual_trigger);

        Register::notificationFromArray(
            [
                'hash' => 'notification_manual_trigger_348m7t5',
                'title' => 'Notification manual trigger',
                'trigger' => $manual_trigger,
                'carriers' => [
                    'email' => [
                        'activated' => true,
                        'enabled' => true,
                        'subject' => 'Manual trigger test',
                        'body' => 'Hello from the other side!',
                        'recipients' => [
                            [
                                'type' => 'administrator',
                                'recipient' => '',
                            ],
                        ],
                    ],
                ],
                'enabled' => true,
                'extras' => [],
                'version' => time(),
            ]
        );
    }
);

In this example, we use a simple email Carrier, but you are free to use any other Carrier registered within the Notification plugin. The easiest way to get the Carrier configuration is to set it up in the WordPress Dashboard and just export the Notification to JSON. The JSON file will have all the keys you need to configure here.

Action

Our Trigger uses notification_manual_trigger_348m7t5 action hook, so the only one thing left is to call it somewhere. The simplest would be:

do_action('notification_manual_trigger_348m7t5');

It can be convenient to wrap this with a simple $_GET param check.

add_action('init', function() {
    if (isset($_GET['i3m84ty'])) {
        do_action('notification_manual_trigger_348m7t5');
    }
});

And call it like this: example.com?i3m84ty

PreviousAllow other roles to edit NotificationsNextBackground Processing filter

Last updated 6 months ago

Was this helpful?

You need to wrap the class definition with notification/init action, because .

Read more about

Read more about

πŸ”§
this is the earliest the abstract classes are accessible
Custom Triggers
Programmatic Notifications