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

Word Press: How to add custom validation to Advanced Custom Fields

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

我试着按照这个 post:

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

但是我要更改我的 word press 文件夹中的什么文件才能使其正常工作?

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

我最近尝试将它放在 validation.php 的 acf 插件文件夹中。

我在这里试过:

In

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 文件、库 类 等...此处需要的所有内容都首先在 wordpress 管理和您的主题中运行。