• Resolved Alex

    (@alexandronalin)


    Hi,

    I’m trying to use this filter to add an array to webhook output:

    https://vikwp.com/support/knowledge-base/vik-appointments/hooks/web-hooks/before-dispatch

    but it seems I’m wrong to code.

    If I use the code below, it works, adding [somekey] => true:

    add_filter('vikappointments_before_dispatch_webhook', function($status, &$payload, &$headers, $hook, $job)
    {
       	$payload = array(
    	"somekey" => true
    	);
    	return $status;
    }, 10, 5);

    but it replaces completely the output payload.

    If I use the code below instead:

    add_filter('vikappointments_before_dispatch_webhook', function($status, &$payload, &$headers, $hook, $job)
    {
         $payload['somekey'] = 'true' );
    
         return $status;
    }, 10, 5);

    no webhook output comes from plugin.

    Have you an idea where I’m wrong?

    Many thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Alex

    (@alexandronalin)

    Sorry. The 2nd code I pasted before was wrong. This is the right one, but as I said it doesn’t work

    add_filter('vikappointments_before_dispatch_webhook', function($status, &$payload, &$headers, $hook, $job)
    {
         $payload['somekey'] = 'true' ;
    
         return $status;
    }, 10, 5);
    Plugin Author e4jvikwp

    (@e4jvikwp)

    Hi Alex,

    as you can see from our documentation, the type of the $payload argument is flagged as mixed. This means that it may vary depending on the webhook that is going to be dispatched.

    According to your example, if you try to assign an attribute by considering the $payload as an array, the system will throw an error in case the latter is actually an object. That’s why the webhook never reaches the end-point you specified.

    I think that you have just to modify a bit your code in order to make sure that you are properly treating the $payload argument according to the provided type. Something like:

    add_filter('vikappointments_before_dispatch_webhook', function($status, &$payload, &$headers, $hook, $job)
    {
         if (is_array($payload))
         {
              $payload['somekey'] = true;
         }
         else if (is_object($payload))
         {
              $payload->somekey = true;
         }
    
         return $status;
    }, 10, 5);
    Thread Starter Alex

    (@alexandronalin)

    Great!

    Many thanks

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘webhook filter’ is closed to new replies.