Filter uncanny-automator

automator_outgoing_webhook_value_of_type_array

Filters the value of a webhook before it's sent, allowing modification based on type and key.

add_filter( 'automator_outgoing_webhook_value_of_type_array', $callback, 10, 3 );

Description

Fires when an array or object value is being prepared for an outgoing webhook. Developers can use this to dynamically change the type of the webhook value (defaulting to 'text') or modify the value itself before it's sent. This hook is essential for ensuring complex data structures are correctly represented in webhook payloads.


Usage

add_filter( 'automator_outgoing_webhook_value_of_type_array', 'your_function_name', 10, 3 );

Parameters

$type (mixed)
This parameter specifies the default data type of the webhook value.
$key (mixed)
This parameter represents the default data type, which is 'text'.
$value (mixed)
This parameter contains the key associated with the value being processed for the outgoing webhook.

Return Value

The filtered value.


Examples

/**
 * Example of how to filter the value of an array type for outgoing webhooks.
 * This function will check if an array value contains specific keys and modify
 * it to be a JSON string representation if it does.
 *
 * @param string $type The current type of the value (default is 'text').
 * @param string $key The key of the webhook data.
 * @param array|object $value The value of the webhook data.
 * @return string|array|object The potentially modified value.
 */
add_filter( 'automator_outgoing_webhook_value_of_type_array', function( $type, $key, $value ) {
    // Check if the array value contains a specific key, e.g., 'user_details'.
    // If it does, we might want to send it as a JSON string instead of a raw array.
    if ( isset( $value['user_details'] ) && is_array( $value['user_details'] ) ) {
        // Encode the specific part of the array to JSON.
        $value['user_details'] = wp_json_encode( $value['user_details'] );
        // Optionally, change the type to 'json' if the webhook system supports it.
        // For this example, we'll keep it as an array and let the system handle JSON encoding.
        // $type = 'json';
    }

    // Return the potentially modified value.
    return $value;
}, 10, 3 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/core/lib/webhooks/class-automator-send-webhook.php:1083

public static function value_maybe_of_type( $key, $value ) {
		$type = 'text';

		if ( is_array( $value ) || is_object( $value ) ) {
			return apply_filters( 'automator_outgoing_webhook_value_of_type_array', $type, $key, $value );
		}

		if ( is_email( $value ) ) {
			$type = 'email';

			return apply_filters( 'automator_outgoing_webhook_value_of_type_email', $type, $key, $value );
		}

		if ( is_float( $value ) ) {
			$type = 'float';

			return apply_filters( 'automator_outgoing_webhook_value_of_type_float', $type, $key, $value );
		}

		if ( is_numeric( $value ) ) {
			$type = 'int';

			return apply_filters( 'automator_outgoing_webhook_value_of_type_int', $type, $key, $value );
		}

		if ( wp_http_validate_url( $value ) ) {
			$type = 'url';

			return apply_filters( 'automator_outgoing_webhook_value_of_type_url', $type, $key, $value );
		}

		return apply_filters( 'automator_outgoing_webhook_value_of_type_text', $type, $key, $value );
	}


Scroll to Top