Webhook launching twice into a snippet

Hello to all,

I have created a snippet that runs every time a seller updates their profile, which sends a webhook to a specific url. I have a problem because when a user updates their profile, this snippet launches twice. Does anyone know why?

Thanks in advance and happy holidays.

Please share the code snippet, do you use some specific HivePress action hook?

here it is. We use the update vendor action.

add_action(
	'hivepress/v1/models/vendor/update',
	function($vendor_id, $vendor) {
		
		//$vendor = HivePress\Models\Vendor::query()->get_by_id($vendor_id);
		
		// Send request.
		wp_remote_post(
			'URL',
			array(
                'body' => array(
					'first_name' =>  wp_get_current_user()->first_name,
					'last_name' =>  wp_get_current_user()->last_name,
					'email' =>  wp_get_current_user()->user_email,
					'enterprise' =>  "stud-book.ch",
					//'month' =>  $vendor->get_month(),
					//'day' =>  $vendor->get_day(),
					//'year' =>  $vendor->get_year(),
					'dateofbirth' => $vendor->get_dob(),
					'city' =>  $vendor->get_city(),
					'address' =>  $vendor->get_address(),
					'phone' =>  $vendor->get_phone(),
                    'iban' =>  $vendor->get_iban(),
					'npa' =>  $vendor->get_zip(),
					
                )
            )
        );
	},
	1000,
	4
);

Please try adding remove_action() | Function | WordPress Developer Resources to unhook the callback before the wp_remove_post function. This way the callback will 100% fire once within the same request.

I try to add this remove_action but it’s not working, make.com receive two webhook.

add_action(
	'hivepress/v1/models/vendor/update',
	function($vendor_id, $vendor) {

		// remove action to execute the callback once
		remove_action( 'hivepress/v1/models/vendor/update', [ hivepress()->attribute, 'update_model_snippet' ], 100 );
		
		// Send request.
		wp_remote_post(
			'URL',
			array(
                'body' => array(
					'first_name' =>  wp_get_current_user()->first_name,
					'last_name' =>  wp_get_current_user()->last_name,
					'email' =>  wp_get_current_user()->user_email,
					'enterprise' =>  "stud-book.ch",
					'dateofbirth' => $vendor->get_dob(),
					'city' =>  $vendor->get_city(),
					'address' =>  $vendor->get_address(),
					'phone' =>  $vendor->get_phone(),
                    'iban' =>  $vendor->get_iban(),
					'npa' =>  $vendor->get_zip(),
					
                )
            )
        );
	},
	1000,
	4
);

I think that when we complete the profile information, the snippet is launched twice because there are two “updated” events, is this possible?

It will not work this way, please name the callback function and remove it using its name, for example:

add_action('hook_name_here', 'my_custom_callback', 10, 2);

function my_custom_callback($vendor_id, $vendor) {
    remove_action('hook_name_here', 'my_custom_callback', 10, 2);

    // custom code here
}

Yes, this hook can be fired a few times during the request, for example if another vendor field is updated after other fields are updated, it also fires the same hook. By removing the callback you can make sure that your custom code runs once.

1 Like

Perfect, thank you very much ! :slight_smile:

1 Like