Filter uncanny-automator

automator_keap_validate_custom_field_value

Filter custom field value. Filters the value of a Keap custom field before it's saved, allowing modification or error handling.

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

Description

Allows developers to intercept and modify the validation of a Keap custom field value before it's processed. This filter is ideal for custom validation logic, data transformation, or returning a `WP_Error` object to halt processing with specific feedback. Access the field ID, original value, and configuration for context.


Usage

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

Parameters

$value (mixed)
- The custom field value or WP_Error.
$field_id (string)
- The custom field key.
$original_value (string)
- The original custom field value.
$config (array)
- The custom field config.

Return Value

mixed


Examples

/**
 * Validate the Keap custom field value to ensure it's a valid email address.
 *
 * This example demonstrates how to hook into 'automator_keap_validate_custom_field_value'
 * to perform specific validation on a custom field being processed for Keap integration.
 * In this case, we're specifically checking if a field intended for an email address
 * actually contains a valid email format.
 *
 * @param mixed $value           The custom field value.
 * @param string $field_id       The custom field key.
 * @param string $original_value The original custom field value.
 * @param array $config          The custom field configuration.
 *
 * @return mixed The validated value or a WP_Error object if validation fails.
 */
function my_automator_keap_validate_email_field( $value, $field_id, $original_value, $config ) {

	// Only validate if the field_id matches our specific email field.
	// Replace 'your_keap_email_field_id' with the actual ID of the Keap custom field
	// that you expect to contain an email address.
	if ( 'your_keap_email_field_id' === $field_id ) {

		// Ensure the value is not empty before validation.
		if ( ! empty( $value ) && ! is_wp_error( $value ) ) {

			// Use WordPress's built-in sanitize_email() for basic email validation.
			// This function returns an empty string for invalid emails.
			$sanitized_email = sanitize_email( $value );

			if ( '' === $sanitized_email ) {
				// If sanitization results in an empty string, it's not a valid email.
				return new WP_Error(
					'automator_keap_invalid_email',
					sprintf(
						__( 'The value for "%1$s" is not a valid email address.', 'your-text-domain' ),
						$field_id
					)
				);
			}

			// If valid, return the sanitized email address.
			return $sanitized_email;
		}
	}

	// If it's not our specific field or the value is already an error or empty,
	// return the original value without modification.
	return $value;
}
add_filter( 'automator_keap_validate_custom_field_value', 'my_automator_keap_validate_email_field', 10, 4 );

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/integrations/keap/helpers/keap-helpers.php:1288

public function validate_custom_field_value( $field_id, $value, $config ) {

		// Stash original value for filters.
		$original_value = $value;

		// Sanitize value by type.
		$value = $this->sanitize_custom_field_value_by_type( $value, $config['type'], $config );

		// Validate value by options.
		if ( ! is_wp_error( $value ) && ! empty( $config['options'] ) ) {
			$value = $this->validate_custom_field_value_by_options( $value, $config );
		}

		/**
		 * Filter custom field value.
		 *
		 * @param mixed $value           - The custom field value or WP_Error.
		 * @param string $field_id       - The custom field key.
		 * @param string $original_value - The original custom field value.
		 * @param array $config          - The custom field config.
		 *
		 * @return mixed
		 */
		$value = apply_filters( 'automator_keap_validate_custom_field_value', $value, $field_id, $original_value, $config );

		return $value;
	}

Scroll to Top