Filter uncanny-automator

automator_send_webhook_xml_body

Filters the XML body for webhook requests, allowing modification of the data sent to external services.

add_filter( 'automator_send_webhook_xml_body', $callback, 10, 1 );

Description

Allows developers to modify the XML body wrapper for webhook requests. By default, it uses a basic `` structure. This filter provides an opportunity to customize the XML output before it's sent, enabling advanced manipulation of the webhook's payload structure.


Usage

add_filter( 'automator_send_webhook_xml_body', 'your_function_name', 10, 1 );

Parameters

$fields (mixed)
This parameter represents the initial XML body wrapper, typically an empty `<body></body>` tag, which will be populated and modified by the filter.

Return Value

The filtered value.


Examples

/**
 * Filters the XML body wrapper for webhooks to include additional attributes.
 *
 * This example demonstrates how to modify the default XML body wrapper,
 * in this case, adding a 'version' attribute to the 'body' tag.
 *
 * @param string $xml_body_wrapper The default XML body wrapper (e.g., '<body></body>').
 * @param array  $fields           The fields to be converted to XML.
 *
 * @return string The modified XML body wrapper with added attributes.
 */
add_filter( 'automator_send_webhook_xml_body', function( $xml_body_wrapper, $fields ) {

	// Ensure $xml_body_wrapper is a string and we have a valid starting tag.
	if ( ! is_string( $xml_body_wrapper ) || strpos( $xml_body_wrapper, '<body' ) !== 0 ) {
		return $xml_body_wrapper; // Return original if it's not the expected format.
	}

	// Add a version attribute to the body tag.
	// In a real-world scenario, you might derive this from $fields or plugin settings.
	$modified_body_wrapper = str_replace( '<body>', '<body version="1.0">', $xml_body_wrapper );

	// You could also conditionally add attributes based on $fields if needed.
	// if ( isset( $fields['some_key'] ) ) {
	//     $modified_body_wrapper = str_replace( '<body>', '<body source="' . esc_attr( $fields['some_key'] ) . '">', $xml_body_wrapper );
	// }

	return $modified_body_wrapper;
}, 10, 2 ); // 10 is the default priority, 2 is the number of accepted arguments.

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:737

$fields = $this->string_to_binary_conversion( $fields );
				break;
			case 'html':
				$fields = $this->build_html_table( $fields, $is_check_sample );
				break;
			case 'xml':
				try {
					$xml_body_wrapper = apply_filters( 'automator_send_webhook_xml_body', '<body></body>', $fields );
					if ( empty( $xml_body_wrapper ) ) {
						$fields = esc_html__( 'XML body wrapper cannot be empty. Please use `automator_send_webhook_xml_body` filter to define a wrapper.', 'uncanny-automator' );
						break;
					}
					$xml_data = new SimpleXMLElement( $xml_body_wrapper );
					$this->array_to_xml( $fields, $xml_data );
					$fields = $xml_data->asXML();


Scroll to Top