LogoLogo
WordPress.orgExtensionsSupport
Version 6
Version 6
  • Notification – Custom Notifications and Alerts for WordPress
  • Known issues
  • User guide
    • How Notification plugin works
    • Who can use this plugin
    • How to create Notifications
    • Custom Post Type support
    • How to escape { character
    • Background processing
    • Troubleshooting
    • How to setup different FROM address for different Notifications
  • Developer
    • General
      • Plugin loading chain
      • Extension possibilities
      • Creating an extension
      • 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
      • Postponing the Trigger action
      • Delaying Trigger execution with Cron
    • Carriers
      • Suppressing the Carrier
    • Snippets
      • General
        • Automatic Trigger testing
        • Allow other roles to edit Notifications
      • Triggers
        • Post
        • User
      • Integations
        • WP All Import
        • MemberPress
        • Gutenberg
  • Extensions
    • Installation
    • Planned extensions
    • 3rd Party Extensions
    • Custom Fields
    • Scheduled Triggers
Powered by GitBook
On this page
  • Postponed Trigger
  • Postponed action callback
  • Troubleshooting
  • Postponed trigger is executed twice

Was this helpful?

  1. Developer
  2. Triggers

Postponing the Trigger action

Action postpone is very useful in some scenarios. It allows you to test some conditions in an earlier action and execute the Trigger in a later one.

Postponed Trigger

Let’s take a look at an example:

class CustomTrigger extends BracketSpace\Notification\Abstracts\Trigger {

	/**
	 * Constructor
	 */
	public function __construct() {

		parent::__construct(
			'myplugin/custom_trigger',
			__( 'Custom Trigger title', 'textdomain' )
		);

		// We are registering our ealier action with 2 arguments.
		$this->add_action( 'earlier_action', 10, 2 );

	}

	/**
	 * Assigns action callback args to object
	 * Return `false` if you want to abort the trigger execution
	 *
	 * @return mixed void or false if no notifications should be sent
	 */
	public function action( $param, $another_param ) {

		$this->this = $param;
		$this->that = $another_param;

		// Postpone this action to a later hook.
		$this->postpone_action( 'later_action', 10, 1 );

	}

}

As you can see we can set any properties in ealier action and execute the trigger later.

Postponed action callback

What if you need another callback argument, passed to the later action? You can use the second method:

/**
 * Postponed action callback
 *
 * Return `false` if you want to abort the trigger execution
 *
 * @return mixed void or false if no notifications should be sent
 */
public function postponed_action( $later_action_param ) {

	// we have access to the new callback args here.
	$this->from_later_action = $later_action_param;

	// you can also abort in the later action by returning false.
	if ( $this->from_later_action === false ) {
		return false;
	}

}

Troubleshooting

Postponed trigger is executed twice

Sometimes an action is done recursively. In this case you have to bail in the postponed_action callback if the action has been already done:

public function postponed_action() {

	// Fix for the action being called recursively.
	if ( did_action( 'postponed_action_hook_name' ) ) {
		return false;
	}

}
PreviousAdding Merge Tags to existing TriggersNextDelaying Trigger execution with Cron

Last updated 6 years ago

Was this helpful?