1. Home
  2. Troubleshooting
  3. My trigger and actions create a loop

My trigger and actions create a loop

Using multiple triggers is not an issue, but combining some of them, results in creating a loop.
As an example, we will take a closer look at the following scenario:

  1. You have a trigger in place that fires once a user is updated
  2. Once the trigger is fired, is calls a webhook action that updates a user

This example will continuously update the user in a loop since once the user is updated, the trigger fires again, which calls the action and so on.

In the rare occurrence of a case like that, we prepared this helpfile to solve this issue.

To get started, the first thing you need to do is to add some further parameters to the webhook URL you use for your webhook action (The URL that causes the loop). Please add to your webhook URL the following:

&stoploop=yes

After you did that, you need to extend your themes functions.php file with the following piece of code.

add_filter( 'wpwhpro/admin/webhooks/is_valid_trigger_response', 'wpwh_stop_update_user_loop', 100, 4 );
function wpwh_stop_update_user_loop( $response, $webhook, $data, $args ){

	$prevent_webhook_triggers = array(
		'update_user' //this is the webhook trigger name you want to prevent from firing
	);

	$webhook_name = ( is_array($webhook) && isset( $webhook['webhook_name'] ) ) ? $webhook['webhook_name'] : '';

	if( isset( $_GET['stoploop'] ) && $_GET['stoploop'] === 'yes' ){
		if( in_array( $webhook_name, $prevent_webhook_triggers ) ){
			$response['is_valid'] = false;
		}
	}

	return $response;
}

In the code above, we prevent the “Send Data On User Update (update_user)” from firing again on the webhook action you just called.

To customize the triggers, you can simply add or remove the trigger from the $prevent_webhook_triggers variable.

That’s it. After you save everything, you are good to go.
This example prevents the Update User trigger from firing again after you update a user using a webhook action.

Updated on October 18, 2021

Was this article helpful?

Related Articles