automator_field_value_sanitized_{$field_code}
Filters the sanitized value for a specific field code. Dynamic hook that allows field-code-specific value modifications. Follows WordPress pattern (e.g., pre_update_option_{$option}). Filters the sanitized value for a specific field code, allowing modifications before the value is used.
add_filter( 'automator_field_value_sanitized_{$field_code}', $callback, 10, 5 );
Description
Fires after a specific field's value has been sanitized. Modify the sanitized value based on its field code, type, or transport. Useful for enforcing custom sanitization rules or transforming data before it's used in automations, especially when interacting with APIs. The original and field objects are also provided for comprehensive control.
Usage
add_filter( 'automator_field_value_sanitized_{$field_code}', 'your_function_name', 10, 5 );
Parameters
-
$sanitized(mixed) - The sanitized value.
-
$original(mixed) - The original value before sanitization.
-
$type(string) - The resolved field type.
-
$transport(string) - The transport identifier (e.g., 'rest', 'mcp').
-
$field(Field) - The field object (for advanced use).
Return Value
The filtered value.
Examples
/**
* Example of how to filter a sanitized field value for a specific field code.
*
* This callback will run after a field's value has been sanitized, specifically for fields
* that have the code 'my_custom_text_field'. It demonstrates how to further process
* or modify the sanitized value if needed.
*/
add_filter( 'automator_field_value_sanitized_my_custom_text_field', function( $sanitized_value, $original_value, $field_type, $transport, $field_object ) {
// For demonstration, let's assume 'my_custom_text_field' is expected to be a string.
// We might want to ensure it's not empty or contains specific disallowed characters.
if ( is_string( $sanitized_value ) ) {
// Example: Trim whitespace and convert to lowercase.
$processed_value = strtolower( trim( $sanitized_value ) );
// Example: If the processed value is empty, we might want to return a default value.
if ( empty( $processed_value ) ) {
// You could return null, an empty string, or a predefined default.
// Let's return an empty string here if it ends up empty after processing.
return '';
}
// You could also perform more complex checks, like validating against a regex.
// For example, disallowing numbers in this specific text field.
if ( preg_match( '/d/', $processed_value ) ) {
// If numbers are found, and we want to disallow them, we could:
// 1. Return an empty string to clear the value.
// 2. Log an error and return the original sanitized value.
// 3. Throw an exception (though filters typically avoid this).
// For this example, let's just return an empty string if numbers are present.
return '';
}
// If all checks pass, return the processed value.
return $processed_value;
}
// If the sanitized value is not a string (e.g., null, array), return it as is.
// The original sanitized value is the safest fallback if our type assumption fails.
return $sanitized_value;
}, 10, 5 ); // Priority 10, accepting 5 arguments.
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/field/class-field-sanitizer.php:132
*
* @param mixed $sanitized The sanitized value.
* @param mixed $original The original value before sanitization.
* @param string $type The resolved field type.
* @param string $transport The transport identifier (e.g., 'rest', 'mcp').
* @param Field $field The field object (for advanced use).
*/
return apply_filters(
"automator_field_value_sanitized_{$field_code}",
$sanitized,
$value,
$type_value,
$transport,
$field
);