The ACF (Advanced Custom Fields) plugin provides a powerful way to add custom fields to your WordPress website. The acf/validate_value
filter allows you to validate and modify the value of a custom field before it is saved to the database. This can be useful in various scenarios, such as enforcing data format or performing calculations based on the field value.
Here’s an example of how to use the acf/validate_value
filter to validate a custom field value and display an error message if the validation fails:
function wpsnippets_validate_custom_field( $valid, $value, $field, $input_name ) {
// Perform your validation logic here
if ( $value < 10 ) {
$valid = false;
$field['message'] = 'The value must be greater than or equal to 10.';
}
return $valid;
}
add_filter( 'acf/validate_value', 'wpsnippets_validate_custom_field', 10, 4 );
In this example, we define a custom function wpsnippets_validate_custom_field
that takes four parameters: $valid
, $value
, $field
, and $input_name
. The $valid
parameter represents the current validation status, $value
contains the value of the custom field, $field
holds the field settings, and $input_name
is the name attribute of the input field.
Within the function, we perform our validation logic. If the value is less than 10, we set $valid
to false
and update the error message in the $field
array. Finally, we return the modified $valid
value.
By adding this filter to your theme’s functions.php
file or a custom plugin, you can ensure that the custom field value meets your specific requirements before it is saved.
Remember to replace wpsnippets_
with your own prefix to maintain code consistency and avoid conflicts with other functions.
Examples
Example 1: Validating and sanitizing a custom field value using the acf/validate_value
filter
This example demonstrates how to use the acf/validate_value
filter to validate and sanitize a custom field value before it is saved to the database. The code example shows how to validate and sanitize a text field value by removing any HTML tags and ensuring it is not empty.
function wpsnippets_validate_text_field( $value, $field, $input ) {
// Remove HTML tags from the value
$value = wp_strip_all_tags( $value );
// Check if the value is empty
if ( empty( $value ) ) {
// Return an error message
return 'The field cannot be empty.';
}
// Return the validated and sanitized value
return $value;
}
add_filter( 'acf/validate_value/type=text', 'wpsnippets_validate_text_field', 10, 3 );
In this code example, the wpsnippets_validate_text_field
function is hooked to the acf/validate_value/type=text
filter. It receives the value of the field, the field object, and the input array as parameters. The function removes any HTML tags from the value using the wp_strip_all_tags
function and checks if the value is empty. If the value is empty, it returns an error message. Otherwise, it returns the validated and sanitized value.
Example 2: Validating and sanitizing a number field value using the acf/validate_value
filter
This example demonstrates how to use the acf/validate_value
filter to validate and sanitize a number field value before it is saved to the database. The code example shows how to validate and sanitize a number field value by ensuring it is a valid numeric value.
function wpsnippets_validate_number_field( $value, $field, $input ) {
// Check if the value is a valid number
if ( ! is_numeric( $value ) ) {
// Return an error message
return 'The field must be a valid number.';
}
// Return the validated and sanitized value
return $value;
}
add_filter( 'acf/validate_value/type=number', 'wpsnippets_validate_number_field', 10, 3 );
In this code example, the wpsnippets_validate_number_field
function is hooked to the acf/validate_value/type=number
filter. It receives the value of the field, the field object, and the input array as parameters. The function checks if the value is a valid number using the is_numeric
function. If the value is not a valid number, it returns an error message. Otherwise, it returns the validated and sanitized value.
Example 3: Validating and sanitizing a select field value using the acf/validate_value
filter
This example demonstrates how to use the acf/validate_value
filter to validate and sanitize a select field value before it is saved to the database. The code example shows how to validate and sanitize a select field value by ensuring it is one of the predefined options.
function wpsnippets_validate_select_field( $value, $field, $input ) {
// Get the available options for the select field
$options = $field['choices'];
// Check if the value is one of the available options
if ( ! in_array( $value, $options ) ) {
// Return an error message
return 'Invalid option selected.';
}
// Return the validated and sanitized value
return $value;
}
add_filter( 'acf/validate_value/type=select', 'wpsnippets_validate_select_field', 10, 3 );
In this code example, the wpsnippets_validate_select_field
function is hooked to the acf/validate_value/type=select
filter. It receives the value of the field, the field object, and the input array as parameters. The function retrieves the available options for the select field from the field object and checks if the value is one of the available options using the in_array
function. If the value is not one of the available options, it returns an error message. Otherwise, it returns the validated and sanitized value.