Filter Since 7.0 uncanny-automator

automator_recipe_{$item_type}_fields_update_before

Filters field config before saving specific item type. Dynamic hook: automator_recipe_{type}_fields_before_save Filters field configuration before saving an item type's fields for a specific recipe.

add_filter( 'automator_recipe_{$item_type}_fields_update_before', $callback, 10, 3 );

Description

Fires before field configuration is saved for a specific recipe item type in the API. Developers can modify the `$config` array to alter field settings, validate data, or perform pre-save actions. This hook is crucial for customizing how item fields are processed and stored within recipes.


Usage

add_filter( 'automator_recipe_{$item_type}_fields_update_before', 'your_function_name', 10, 3 );

Parameters

$config (array)
Flattened field config.
$recipe_id (int)
Recipe ID.
$item_id (int|string)
Item ID.

Return Value

The filtered value.


Examples

/**
 * Modify the configuration of a specific trigger field before it's saved.
 *
 * For example, this could be used to dynamically set a default value for a field
 * based on other fields or the recipe context.
 *
 * @param array $config The flattened field configuration.
 * @param int   $recipe_id The ID of the recipe.
 * @param int|string $item_id The ID of the item (trigger or action).
 * @return array The modified field configuration.
 */
add_filter( 'automator_recipe_trigger_fields_update_before', function( $config, $recipe_id, $item_id ) {
    // Let's assume we want to modify a field named 'specific_trigger_field'
    // and set its value to 'new_default_value' if it's empty.
    $field_to_modify_key = 'specific_trigger_field';

    // Check if the field we want to modify exists in the config
    if ( isset( $config[ $field_to_modify_key ] ) ) {
        // Ensure the field has a 'value' key and it's currently empty or not set
        if ( ! isset( $config[ $field_to_modify_key ]['value'] ) || empty( $config[ $field_to_modify_key ]['value'] ) ) {
            // Set a new default value for the field
            $config[ $field_to_modify_key ]['value'] = 'new_default_value_for_recipe_' . $recipe_id;
        }
    }

    // You could also add validation or modify other properties of the field config.
    // For instance, disabling a field based on other settings:
    // if ( isset( $config['another_field']['value'] ) && $config['another_field']['value'] === 'disable_this' ) {
    //     $config[ $field_to_modify_key ]['disabled'] = true;
    // }

    return $config;
}, 10, 3 );

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/transports/restful/recipe/items/traits/filters/trait-filter-field-values-before-save.php:78

private function apply_fields_update_before_filters( array $config ): array {
		$item_type = $this->get_item_type();

		/**
		 * Filters field config before saving any recipe item.
		 *
		 * @since 7.0
		 *
		 * @param array      $config    Flattened field config.
		 * @param int        $recipe_id Recipe ID.
		 * @param int|string $item_id   Item ID (int for posts, string for conditions).
		 * @param string     $item_type Item type (trigger, action, closure, filter_condition).
		 */
		$config = apply_filters(
			'automator_recipe_item_fields_update_before',
			$config,
			$this->get_recipe_id(),
			$this->get_item_id(),
			$item_type
		);

		/**
		 * Filters field config before saving specific item type.
		 *
		 * Dynamic hook: automator_recipe_{type}_fields_before_save
		 *
		 * @since 7.0
		 *
		 * @param array      $config    Flattened field config.
		 * @param int        $recipe_id Recipe ID.
		 * @param int|string $item_id   Item ID.
		 */
		$config = apply_filters(
			"automator_recipe_{$item_type}_fields_update_before",
			$config,
			$this->get_recipe_id(),
			$this->get_item_id()
		);

		return $config;
	}


Scroll to Top