Word Press:如何将自定义验证添加到高级自定义字段



我正在尝试使用这个插件:高级自定义字段,并为特定字段添加我自己的验证。

我试着关注这个帖子:

https://www.advancedcustomfields.com/resources/acf-validate_value/

但是,我应该更改word press文件夹中的哪个文件才能使其工作?

我正在尝试验证一个匹配1234的Pin#。

我最近试着把它放在acf插件文件夹中进行验证。php.

我在这里试过了:

在中

function acf_validate_value( $value, $field, $input ) {
// vars
$valid   = true;
$message = sprintf( __( '%s value is required', 'acf' ), $field['label'] );
// valid
if ( $field['required'] ) {
// valid is set to false if the value is empty, but allow 0 as a valid value
if ( empty( $value ) && ! is_numeric( $value ) ) {
$valid = false;
}
}
/**
*  Filters whether the value is valid.
*
*  @date    28/09/13
*  @since   5.0.0
*
*  @param   bool $valid The valid status. Return a string to display a custom error message.
*  @param   mixed $value The value.
*  @param   array $field The field array.
*  @param   string $input The input element's name attribute.
*/
$valid = apply_filters( "acf/validate_value/type={$field['type']}", $valid, $value, $field, $input );
$valid = apply_filters( "acf/validate_value/name={$field['_name']}", $valid, $value, $field, $input );
$valid = apply_filters( "acf/validate_value/key={$field['key']}", $valid, $value, $field, $input );
$valid = apply_filters( 'acf/validate_value', $valid, $value, $field, $input );
$valid = add_filter('acf/validate_value/name=registration_pin', 'my_acf_validate_value', 10, 4);
// allow $valid to be a custom error message
if ( ! empty( $valid ) && is_string( $valid ) ) {
$message = $valid;
$valid   = false;
}
if ( ! $valid ) {
acf_add_validation_error( $input, $message );
return false;
}
// return
return true;
}
function my_acf_validation_registation_pin($valid, $value, $field, $input_name ){
// Bail early if value is already invalid.
if( $valid !== true ) {
return $valid;
}
console.log('validate registration pin: test ->' + new Date());
}

是的,您的代码无法工作。add_filter将被击中太晚。

如果您只是试图获得一个acf字段,以根据定义的集合值验证提交的值。

最好的方法是将其添加到您的functions.php中,请参阅下面代码中的注释。。。

<?php
// add validate value filter for all acf fields with the field name `registration_pin`
// you can use `acf/validate_value/key=` to target a specific acf field by key if you have multiple fields with the same name `registration_pin` in different field groups
add_filter('acf/validate_value/name=registration_pin', 'acf_validate_reg_pin', 10, 4);
// function used by the above `add_filter`
function acf_validate_reg_pin($valid, $value, $field, $input_name) {
// define our registration pin to validate submitted value against
$reg_pin = '1234';
// if submitted value is empty or false
// remove this condition if you want the form to still validate if the field value is empty/false
if(!$value) {
// field returns invalid and show this error message
return __('Please enter registration pin.');
}
// if submitted value is a string and is not equal to our defined registration pin
if(is_string($value) && $value !== $reg_pin) {
// field returns invalid and show this error message
return __('Incorrect registration pin.');
}
// return field as valid, if none of the above conditions are true
return $valid;
}

这可能会让您更清楚地了解如何处理特定acf字段的验证。你应该在你的functions.php中使用这个add_filter,这样过滤器就会被击中。

如果您想在函数中保持整洁,可以在functions.php中使用require_once(__DIR__ . '/folder/example.php');。您可以加载php文件、lib类等…这里所需的一切都首先在wordpress admin和您的主题中运行。

最新更新