1. Home
  2. Troubleshooting
  3. My trigger fires multiple times

My trigger fires multiple times

In some cases, it can happen that a trigger fires multiple times with very similar data.
This is usually caused by third-party plugins that use extra code that fires the trigger again, which causes the trigger to fire again.

An example:
You update a post and the post triggers multiple times the webhook to fire. This is often caused by the third-party plugin due to their code as it edits the post again and re-saves it.
Technically, our plugin does everything correctly with firing the trigger multiple times, but often it’s not your desired behavior.

To get rid of that, we offer you two possibilities which you can use to prevent that for most cases:

1. Webhook URL setting (Since 4.3.2)

The newly introduced webhook URL setting called Fire only once per instance, allows you to prevent duplicated calls of the same trigger within the current request (instance) of WordPress. This means that if a plugin triggers a webhook multiple times on a single page call, the other endpoints get prevented.

To use this feature, simply add a webhook URL to your chosen trigger and open its settings.

2. Re-filter all triggers via a PHP snippet (Since 4.2.0)

If you’d like to use that feature, simply copy-paste the code below to your functions.php file of your theme. Once that’s done, simply adjust the trigger you want to filter so that it only fires once. You can do that within the $triggers_to_fire_once variable.

add_filter( 'wpwhpro/post_delay/post_delay_triggers', 'wpwh_fire_trigger_only_once_per_instance', 20 );
function wpwh_fire_trigger_only_once_per_instance( $triggers ){

	//Define the triggers you want to fire only once here (e.g. post_update)
	$triggers_to_fire_once = array(
		'post_update',
	);

	$uniquified_triggers = array();

	foreach( $triggers as $key => $trigger ){
		if( isset( $trigger['options'] ) && isset( $trigger['options']['trigger_name'] ) ){
			if( in_array( $trigger['options']['trigger_name'], $triggers_to_fire_once ) ){

				if( ! isset( $uniquified_triggers[ $trigger['options']['trigger_name'] ] ) ){
					$uniquified_triggers[ $trigger['options']['trigger_name'] ] = array();
				}

				$uniquified_triggers[ $trigger['options']['trigger_name'] ][ $key ] = $trigger;
				unset( $triggers[ $key ] );

			}
		}
	}

	if( ! empty( $uniquified_triggers ) ){
		foreach( $uniquified_triggers as $uniquified_trigger_name => $uniquified_triggers ){
			$reversed_triggers = array_reverse( $uniquified_triggers );

			//Fetch only the last trigger registered
			foreach( $reversed_triggers as $key => $trigger ){
				$triggers[ $key ] = $trigger; //assign the same key again
				break;
			}
		}
	}

	return $triggers;
}

Once the code above was added, it will filter all added triggers (provided that they support post-delayed features) and only fire the latest trigger (the one that was triggered the last).

Updated on January 3, 2022

Was this article helpful?

Related Articles