Filter Since 7.0 uncanny-automator

automator_recipe_item_fields_update_before

Filters field config before saving any recipe item. Filters field configuration before saving any recipe item, allowing modifications to its settings.

add_filter( 'automator_recipe_item_fields_update_before', $callback, 10, 4 );

Description

Fires just before a recipe item's field configuration is saved. Developers can use this filter to modify or validate the flattened field configuration array, recipe ID, item ID, and item type. This hook is useful for custom data manipulation or to prevent invalid configurations from being stored.


Usage

add_filter( 'automator_recipe_item_fields_update_before', 'your_function_name', 10, 4 );

Parameters

$config (array)
Flattened field config.
$recipe_id (int)
Recipe ID.
$item_id (int|string)
Item ID (int for posts, string for conditions).
$item_type (string)
Item type (trigger, action, closure, filter_condition).

Return Value

The filtered value.


Examples

add_filter( 'automator_recipe_item_fields_update_before', 'my_automator_modify_recipe_fields_before_save', 10, 4 );

/**
 * Example filter to modify recipe item fields before they are updated.
 *
 * This function demonstrates how to access and potentially alter the configuration
 * of recipe item fields just before they are saved to the database.
 * For instance, you might want to sanitize certain input values or add default
 * values based on the item type or recipe ID.
 *
 * @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).
 *
 * @return array The potentially modified configuration array.
 */
function my_automator_modify_recipe_fields_before_save( $config, $recipe_id, $item_id, $item_type ) {

	// Example: If the item type is an 'action' and it's a specific action ID,
	// let's ensure a particular field has a default value if it's empty.
	if ( 'action' === $item_type && isset( $config['action_type']['value'] ) && 'send_email' === $config['action_type']['value'] ) {
		if ( isset( $config['email_subject']['value'] ) && empty( $config['email_subject']['value'] ) ) {
			$config['email_subject']['value'] = __( 'New Activity Notification', 'your-text-domain' );
		}
	}

	// Example: Sanitize a specific field value if it exists and is a string.
	if ( isset( $config['custom_message']['value'] ) && is_string( $config['custom_message']['value'] ) ) {
		$config['custom_message']['value'] = sanitize_text_field( $config['custom_message']['value'] );
	}

	// Always return the config, whether modified or not.
	return $config;
}

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:59

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