automator_recipe_before_update
Fires before a recipe is updated, providing access to the recipe item and request data.
add_action( 'automator_recipe_before_update', $callback, 10, 2 );
Description
Fires before a WordPress recipe is updated. This hook allows you to perform actions or modify data immediately before the recipe is saved to the database, providing a crucial point for custom logic and integrations.
Usage
add_action( 'automator_recipe_before_update', 'your_function_name', 10, 2 );
Parameters
-
$item(mixed) - This parameter contains the WordPress post object for the recipe being updated.
-
$request(mixed) - This parameter contains the WordPress post object representing the recipe being updated.
Examples
// Example of how to hook into the automator_recipe_before_update action.
// This function will log the recipe ID and the request data before the recipe is updated.
function my_automator_recipe_log_before_update( $item, $request ) {
// Check if $item is a WP_Post object and has an ID.
if ( is_a( $item, 'WP_Post' ) && isset( $item->ID ) ) {
// Log the recipe ID.
error_log( "Automator Recipe Before Update: Recipe ID = " . $item->ID );
// Log the request data if it exists.
if ( ! empty( $request ) && is_array( $request ) ) {
error_log( "Automator Recipe Before Update: Request Data = " . print_r( $request, true ) );
}
}
}
add_action( 'automator_recipe_before_update', 'my_automator_recipe_log_before_update', 10, 2 );
// Example of modifying the recipe meta data before it's saved.
// This function would, for instance, add a timestamp to a specific meta key.
function my_automator_recipe_add_timestamp_meta( $item, $request ) {
// Assuming we want to add/update a meta key called 'automator_last_updated'
$meta_key_to_update = 'automator_last_updated';
$current_timestamp = current_time( 'mysql' );
// Get the current value of the meta key to avoid overwriting if not intended
$existing_meta_value = get_post_meta( $item->ID, $meta_key_to_update, true );
// Only update if the value is different or if it doesn't exist
if ( $existing_meta_value !== $current_timestamp ) {
update_post_meta( $item->ID, $meta_key_to_update, $current_timestamp );
error_log( "Automator Recipe Before Update: Updated meta key '{$meta_key_to_update}' for Recipe ID {$item->ID} to {$current_timestamp}." );
}
}
add_action( 'automator_recipe_before_update', 'my_automator_recipe_add_timestamp_meta', 10, 2 );
// Example of performing a validation check before the recipe is updated.
// If validation fails, you might want to prevent the update or trigger an error.
// For this example, we'll just log a warning if a specific condition is met.
function my_automator_recipe_validation_check( $item, $request ) {
// Let's imagine we want to check if a recipe has a specific trigger attached
// and log a warning if it doesn't, as this might indicate an incomplete setup.
// This is a hypothetical check and would require more context of how triggers are stored.
// Example: Check if $request contains data that would indicate the trigger is missing.
// In a real scenario, you would query post meta or other related data.
$trigger_data = isset( $request['meta_input']['recipe_triggers'] ) ? $request['meta_input']['recipe_triggers'] : false;
if ( ! $trigger_data || empty( $trigger_data ) ) {
error_log( "Automator Recipe Before Update Warning: Recipe ID {$item->ID} appears to be missing trigger configuration. Request data: " . print_r( $request, true ) );
// In a real-world scenario, you might want to:
// wp_die( "Validation failed: Recipe must have a trigger configured." );
// or throw an exception.
}
}
add_action( 'automator_recipe_before_update', 'my_automator_recipe_validation_check', 10, 2 );
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/automator-post-types/uo-recipe/class-recipe-post-rest-api.php:706
// Make sure the parent post exists
$item = get_post( $item_id );
if ( $item ) {
// @since 6.0
do_action( 'automator_recipe_before_update', $item, $request );
$before_update_value = get_post_meta( $item_id, $meta_key, true );
if ( is_array( $meta_value ) ) {
// Allow integrations to hook into the filter.
$meta_value = apply_filters( 'automator_field_values_before_save', $meta_value, $item );
Internal Usage
Found in uncanny-automator-pro/src/integrations/loopable-rss/loopable-rss-integration.php:41:
add_action( 'automator_recipe_before_update', array( Rss_Content::class, 'on_update_save_content' ), 10, 2 );
Found in uncanny-automator-pro/src/integrations/loopable-json/loopable-json-integration.php:41:
add_action( 'automator_recipe_before_update', array( Json_Content::class, 'on_update_save_content' ), 10, 2 );
Found in uncanny-automator-pro/src/integrations/loopable-csv/loopable-csv-integration.php:41:
add_action( 'automator_recipe_before_update', array( Csv_Content::class, 'on_update_save_content' ), 10, 2 );
Found in uncanny-automator-pro/src/integrations/loopable-xml/loopable-xml-integration.php:42:
add_action( 'automator_recipe_before_update', array( Xml_Content::class, 'on_update_save_content' ), 10, 2 );