Filter Dynamic uncanny-automator

automator_{dynamic}_api_call

> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters the parameters of an API call made by the Automator integration.

add_filter( 'automator_{dynamic}_api_call', $callback, 10, 1 );

Description

This filter hook allows developers to modify the parameters passed to an API call within the core Automator functionality. The specific hook name is dynamic, including the integration name and optionally the action within the integration. Use it to alter or add to the API request data before it's processed.


Usage

add_filter( 'automator_{dynamic}_api_call', 'your_function_name', 10, 1 );

Parameters

$params (mixed)
This parameter contains the arguments passed to the API call, which can be filtered and modified by various hooks.

Return Value

The filtered value.


Examples

add_filter( 'automator_my_custom_integration_api_call', 'my_custom_automator_api_call_params', 10, 1 );

/**
 * Example of modifying parameters for a specific integration's API call.
 *
 * This function hooks into the 'automator_{integration}_api_call' filter.
 * It demonstrates how to access and potentially modify the parameters
 * being passed to an API call for a hypothetical 'my_custom_integration'.
 *
 * For instance, you might want to add a specific API key, modify a request
 * body field, or change the endpoint based on certain conditions.
 *
 * @param array $params The original array of parameters for the API call.
 *                      Expected structure might include 'integration', 'body', 'headers', etc.
 * @return array The modified (or original) array of parameters.
 */
function my_custom_automator_api_call_params( $params ) {
    // Check if this is indeed for our custom integration (though the hook implies it)
    if ( ! isset( $params['integration'] ) || 'my_custom_integration' !== $params['integration'] ) {
        return $params;
    }

    // Example: Add a custom header for authentication
    if ( ! isset( $params['headers'] ) || ! is_array( $params['headers'] ) ) {
        $params['headers'] = array();
    }
    $params['headers']['X-Custom-Auth'] = 'your-secret-api-token';

    // Example: Modify a value in the request body if it exists
    if ( isset( $params['body']['user_id'] ) && ! empty( $params['body']['user_id'] ) ) {
        // Perhaps prepend a prefix to the user ID for this integration
        $params['body']['user_id'] = 'cust_' . $params['body']['user_id'];
    }

    // Example: Add a default value if a specific field is missing
    if ( ! isset( $params['body']['status'] ) ) {
        $params['body']['status'] = 'pending';
    }

    // Always return the modified (or original) parameters array
    return $params;
}

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/classes/class-api-server.php:331

public function filter_params( $params ) {

		$params = apply_filters( 'automator_api_call', $params );

		if ( ! empty( $params['integration'] ) ) {
			$params = apply_filters( 'automator_' . $params['integration'] . '_api_call', $params );

			if ( ! empty( $params['body']['action'] ) ) {
				$params = apply_filters( 'automator_' . $params['integration'] . '_' . $params['body']['action'] . '_api_call', $params );
			}
		}

		return $params;
	}


Scroll to Top