wpwhpro/admin/webhooks/is_valid_trigger_response
Description:
This filter allows you to re-validate the currently called webhook regarding its settings. You can for example permit or restrict the webhook from being fired.
Parameter | Type | Description |
---|---|---|
$response | array |
The response data, that gets validated. You will find an example down below. |
$webhook | array |
An array containing all of the available webhook data |
$data | array |
The data that will be sent with the webhook. |
$args | array |
Additional arguments for the post call. |
This is how the response itself looks. If you set is_valid to true, the webhook will be fired.
$response = array( 'success' => false, 'is_valid' => true, );
$response = apply_filters( 'wpwhpro/admin/webhooks/is_valid_trigger_response', $response, $webhook, $data, $args );
Example
This example will demonstrate to you how to use this filter. As seen below, it will only be validated if the called webhook trigger is of the type post_updated, and the added webhook URL (The endpoint you added within the “Send Data” tab) is equal to my-url-name.
The webhook then is validated against the $data variable (which is the payload sent to your webhook endpoint) and if it is empty, the code below will prevent the webhook from being sent.
add_filter( 'wpwhpro/admin/webhooks/is_valid_trigger_response', 'wpwh_validate_webhook_trigger', 20, 5 ); function wpwh_validate_webhook_trigger( $response, $webhook, $data, $args ){ $webhook_name = 'post_updated'; //set this to your chosen trigger endpoint $webhook_url_name = 'my-url-name'; //set this to your chosen webhook URL name $inc_webhook_name = ( is_array($webhook) && isset( $webhook['webhook_name'] ) ) ? $webhook['webhook_name'] : ''; $inc_webhook_url_name = ( is_array($webhook) && isset( $webhook['webhook_url_name'] ) ) ? $webhook['webhook_url_name'] : ''; //fires the webhook only if the trigger is equivaent to $webhook_name and the webhook trigger URL to $webhook_url_name if( $webhook_name !== $inc_webhook_name || $webhook_url_name !== $inc_webhook_url_name ){ return $data; } //prevent the webhook from firing if the data is empty if( empty( $data ) ){ $response['is_valid'] = false; } return $data; }