automator_{dynamic}_{dynamic}_api_call
> **Note:** This is a dynamic hook. The actual hook name is constructed at runtime. Filters dynamic API call parameters before the API request is made for integrations.
add_filter( 'automator_{dynamic}_{dynamic}_api_call', $callback, 10, 1 );
Description
This dynamic filter hook, automator_{dynamic}_{dynamic}_api_call, is applied just before an API call is processed. It allows developers to intercept and modify the parameters being sent to or received from an API. The hook's name is constructed based on the integration and action being performed, providing granular control for specific API interactions. This enables modifications to request data or response handling for custom integrations.
Usage
add_filter( 'automator_{dynamic}_{dynamic}_api_call', 'your_function_name', 10, 1 );
Parameters
-
$params(mixed) - This parameter contains the arguments for the API call, which can be modified by various filters.
Return Value
The filtered value.
Examples
// Example: Modify the parameters for a specific integration's API call.
// This filter targets the 'automator_google_sheets_api_call' hook,
// assuming 'integration' is 'google_sheets'.
add_filter(
'automator_google_sheets_api_call',
function( $params ) {
// Let's say we want to ensure a specific header is always present
// for Google Sheets API calls made through the automator.
if ( isset( $params['headers'] ) && is_array( $params['headers'] ) ) {
$params['headers']['X-Automator-Source'] = 'WordPress Plugin';
} else {
$params['headers'] = [ 'X-Automator-Source' => 'WordPress Plugin' ];
}
// You could also modify the request body, for instance,
// to add a default value or preprocess data.
if ( ! empty( $params['body'] ) && isset( $params['body']['data'] ) ) {
// Example: Add a timestamp to the data being sent.
$params['body']['data']['timestamp'] = current_time( 'mysql' );
}
// Always return the modified (or unmodified) $params array.
return $params;
},
10, // Priority: Default priority is 10.
1 // Accepted args: The callback function accepts only one argument ($params).
);
// Example: Modify parameters for a specific action within an integration.
// This filter targets the 'automator_slack_send_message_api_call' hook,
// assuming 'integration' is 'slack' and 'action' is 'send_message'.
add_filter(
'automator_slack_send_message_api_call',
function( $params ) {
// For Slack messages, let's enforce a default username if not provided.
if ( ! empty( $params['body']['payload'] ) && is_array( $params['body']['payload'] ) ) {
if ( ! isset( $params['body']['payload']['username'] ) ) {
$params['body']['payload']['username'] = 'WordPress Automator Bot';
}
// Example: Add a custom emoji to the bot's avatar for this specific action.
if ( ! isset( $params['body']['payload']['icon_emoji'] ) ) {
$params['body']['payload']['icon_emoji'] = ':robot_face:';
}
}
// Ensure the modified parameters are returned.
return $params;
},
10, // Priority
1 // Accepted args
);
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:334
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;
}