让我们穿过acf/validate_value 逻辑筛选。。。
我假设您的两个代码段都是相同的代码段,并且您没有同时执行它们(如@Buttered_Toast 指出)。
这是您的代码(请参阅我添加的注释)
下面的代码假设dfg_service_type
acf字段是数字字段
/**
* @param mixed $valid Whether or not the value is valid (boolean) or a custom error message (string).
* @param mixed $value The field value.
* @param array $field The field array containing all settings.
* @param string $input_name The field DOM element name attribute.
* @return bool|string
*/
function dfg_acf_validate_value($valid, $value, $field, $input_name) {
// $valid true by default
// but if $field[\'required\'] is true then
// valid is set to false if the value is empty, but allow 0 as a valid value
if( $valid !== true ) {
return $valid;
}
// set value if value is true and cast value to integer else set bool false
$value = $value ? (int)$value : false;
// here is your first snippet cleaned up...
// you are simply just checking if value is not 30 and not 360
// if anything but 30 or 360, the below condition will return __( \'Please choose a valid type!\' );
// is this your intention?
if( $value && ( $value !== 30 && $value !== 360 )) {
return __( \'Please choose a valid type!\' );
}
/**
// here is another option to check if $value is greater than or equal too 30 and less than or equal too 360...
if( $value && ( $value >= 30 && $value <= 360 )) {
return __( \'Please choose a valid type!\' );
}
*/
// return valid and save value if above value conditions are not run
return $valid;
}
// init the filter perform custom validation on the field’s `$value` before it is saved into the database.
// for all acf fields with the name `dfg_service_type`
add_filter(\'acf/validate_value/name=dfg_service_type\', \'dfg_acf_validate_value\', 10, 4);
更新
让我们试着调试一下。
将一些调试函数添加到functions.php
哪个转储然后退出。。。
假设您的php版本是5.6以上,所以variable-length arg lists 代币工程
<?php
/**
* functions.php
*/
// dump and exit function
function dd(...$args) {
dump(...$args);
exit;
}
// dump
function dump(...$args) {
foreach ($args as $dump) {
echo \'<pre class="dd">\' . print_r($dump, true) . \'</pre>\';
}
}
然后尝试此操作并保存字段。
function dfg_acf_validate_value($valid, $value, $field, $input_name) {
// test this is getting hit
dd($valid, $value, $field, $input_name);
}
add_filter(\'acf/validate_value/name=dfg_service_type\', \'dfg_acf_validate_value\', 10, 4);
如果返回
dd
结果然后命中字段验证值。
让我知道你进展如何,有什么回报,我会尽力帮助你。