automator_trigger_response
Filters the API trigger response before it is returned, allowing modifications based on recipe, saved trigger, and trigger code.
add_filter( 'automator_trigger_response', $callback, 10, 4 );
Description
Fires after a trigger's API response is generated, allowing modification of the `$response` before it's returned. Developers can use this to alter the data sent back to the API, for example, to add or remove fields based on the `$recipe_id`, `$saved_trigger_id`, or `$trigger_code`.
Usage
add_filter( 'automator_trigger_response', 'your_function_name', 10, 4 );
Parameters
-
$response(mixed) - This parameter holds the response data that will be returned after the trigger action has been executed.
-
$recipe_id(mixed) - This parameter contains the response data for the trigger, which can be modified by the filter.
-
$saved_trigger_id(mixed) - This parameter contains the unique identifier for the Automator recipe that the trigger belongs to.
-
$trigger_code(mixed) - This parameter contains the unique identifier of the saved trigger associated with the recipe.
Return Value
The filtered value.
Examples
add_filter( 'automator_trigger_response', 'my_custom_automator_trigger_response_handler', 10, 4 );
/**
* Example handler for the 'automator_trigger_response' filter.
* This function demonstrates how to modify the response based on trigger details.
* For example, it could add additional data to the response or alter its format.
*
* @param mixed $response The original response data from the trigger.
* @param int $recipe_id The ID of the current recipe.
* @param int $saved_trigger_id The ID of the saved trigger instance.
* @param string $trigger_code The code identifying the trigger type.
*
* @return mixed The modified response data.
*/
function my_custom_automator_trigger_response_handler( $response, $recipe_id, $saved_trigger_id, $trigger_code ) {
// Check if the response is already an error, if so, return it as is.
if ( is_wp_error( $response ) ) {
return $response;
}
// Example: Add custom data to the response if it's a specific trigger type.
if ( 'MY_CUSTOM_TRIGGER_CODE' === $trigger_code ) {
// Assume $response is an array or an object that can be modified.
// For demonstration, let's assume it's an array.
if ( is_array( $response ) ) {
$response['custom_message'] = sprintf(
__( 'Trigger "%s" processed successfully for recipe ID %d.', 'your-text-domain' ),
$trigger_code,
$recipe_id
);
// You might also fetch additional data here based on $saved_trigger_id or $recipe_id
// For instance, get user data related to the trigger.
// $user_id = get_post_meta( $saved_trigger_id, '_user_id', true );
// if ( $user_id ) {
// $user_info = get_userdata( $user_id );
// if ( $user_info ) {
// $response['user_email'] = $user_info->user_email;
// }
// }
} elseif ( is_object( $response ) ) {
// If response is an object, you'd access its properties accordingly.
// $response->custom_message = sprintf( __( 'Trigger "%s" processed successfully for recipe ID %d.', 'your-text-domain' ), $trigger_code, $recipe_id );
}
}
// Example: Modify the response for all triggers if a certain condition is met.
// For instance, if we always want to include the recipe title.
// $recipe_title = get_the_title( $recipe_id );
// if ( is_array( $response ) ) {
// $response['recipe_title'] = $recipe_title;
// } elseif ( is_object( $response ) ) {
// $response->recipe_title = $recipe_title;
// }
// Always return the modified response.
return $response;
}
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/api/services/trigger/services/class-trigger-crud-service.php:304
src/api/services/trigger/services/class-trigger-crud-service.php:444
src/api/services/trigger/services/class-trigger-crud-service.php:669
// the CRUD service stays oblivious to what they do.
$saved_trigger_id = $saved_trigger->get_trigger_id()
? (int) $saved_trigger->get_trigger_id()->get_value()
: 0;
if ( $saved_trigger_id > 0 ) {
do_action( 'automator_trigger_saved', $recipe_id, $saved_trigger_id, $trigger_code );
$response = apply_filters( 'automator_trigger_response', $response, $recipe_id, $saved_trigger_id, $trigger_code );
}
return $response;
} catch ( Throwable $e ) {
return new WP_Error(
'trigger_creation_failed',
Internal Usage
Found in uncanny-automator-pro/src/core/webhook/class-webhook-url-handler.php:58:
add_filter( 'automator_trigger_response', array( $this, 'enrich_response' ), 10, 4 );