使用do_action_ref_array帮助回调

时间:2017-07-10 作者:Robin Andrews

通过修改一些插件代码,我成功地获得了我想要的行为。然而,我想使用提供的钩子将我的修改移到插件代码之外,但我似乎无法使其工作。挂钩使用do\\u action\\u ref\\u数组。

我能找到的访问php变量的唯一方法是向调试程序发送错误消息,这一事实无助于这种情况。日志文件。

(像这样):

function ra_func1($args) {
    $str = print_r($args, true);
    error_log($str);
}
add_action ( \'bookly_validate_custom_field\', \'ra_func1\', 10, 3);
问题是对象和数组混合在一起,我现在真的被卡住了,我花了很多时间试图让它工作。如果有人能提供一个符合我要求的工作回调,我将不胜感激。

原始插件代码:

public function validateCustomFields( $value, $form_id, $cart_key )
    {
        $decoded_value = json_decode( $value );
        $fields = array();
        foreach ( json_decode( get_option( \'bookly_custom_fields\' ) ) as $field ) {
            $fields[ $field->id ] = $field;
        }

        foreach ( $decoded_value as $field ) {
            if ( isset( $fields[ $field->id ] ) ) {
                if ( ( $fields[ $field->id ]->type == \'captcha\' ) && ! Captcha\\Captcha::validate( $form_id, $field->value ) ) {
                    $this->errors[\'custom_fields\'][ $cart_key ][ $field->id ] = __( \'Incorrect code\', \'bookly\' );
                } elseif ( $fields[ $field->id ]->required && empty ( $field->value ) && $field->value != \'0\' ) {
                    $this->errors[\'custom_fields\'][ $cart_key ][ $field->id ] = __( \'Required\', \'bookly\' );
                } else {
                    /**
                     * Custom field validation for a third party,
                     * if the value is not valid then please add an error message like in the above example.
                     *
                     * @param \\stdClass
                     * @param ref array
                     * @param string
                     * @param \\stdClass
                     */
                    do_action_ref_array( \'bookly_validate_custom_field\', array( $field, &$this->errors, $cart_key, $fields[ $field->id ] ) );
                }
            }
        }
    }
代码中包含我要通过外部回调添加的功能的开头:

public function validateCustomFields( $value, $form_id, $cart_key )
    {
        $decoded_value = json_decode( $value );
        $fields = array();
        foreach ( json_decode( get_option( \'bookly_custom_fields\' ) ) as $field ) {
            $fields[ $field->id ] = $field;
        }

        foreach ( $decoded_value as $field ) {
            if ( isset( $fields[ $field->id ] ) ) {
                if ( ( $fields[ $field->id ]->type == \'captcha\' ) && ! Captcha\\Captcha::validate( $form_id, $field->value ) ) {
                    $this->errors[\'custom_fields\'][ $cart_key ][ $field->id ] = __( \'Incorrect code\', \'bookly\' );
                } elseif ( $fields[ $field->id ]->required && empty ( $field->value ) && $field->value != \'0\' ) {
                    $this->errors[\'custom_fields\'][ $cart_key ][ $field->id ] = __( \'Required\', \'bookly\' );
                } else {
                    /**
                     * Custom field validation for a third party,
                     * if the value is not valid then please add an error message like in the above example.
                     *
                     * @param \\stdClass
                     * @param ref array
                     * @param string
                     * @param \\stdClass
                     */

                    //

                    if ($fields[$field->id]->id == 12372){
                     $this->errors[\'custom_fields\'][ $cart_key ][ $field->id ] = __( \'Post Code Error\', \'bookly\' );                      
                                         }
                }
            }
        }
    }

2 个回复
最合适的回答,由SO网友:rtpHarry 整理而成

该功能的存在是因为我对他们进行了窃听以实现它:)

使用它也让我感到困惑,但就在前几天,终于有人用这个片段回答了这个问题:

add_action( \'bookly_validate_custom_field\', function ( \\stdClass $field, &$errors, $cart_key, \\stdClass $field_info ) {
    // Validation by custom_field id
    switch ( $field->id ) {
        case \'id_value\':
            if ( /*$invalid == */ true ) {
                $errors[\'custom_fields\'][ $cart_key ][ $field->id ] = __( \'Invalid\', \'bookly\' );
            }
            break;
    }
}, 10, 4 );

SO网友:Syed Abuthahir M

您需要在函数中编写自定义函数。php,名称为bookly_validate_custom_field 并将5个参数传递给该函数

例如

function bookly_validate_custom_field ( $field, &$errors, $cart_key, $field_id ) {
if ($field_id] == 12372){
$errors[\'custom_fields\'][ $cart_key ][ $field_id ] = __( \'Post Code Error\', \'bookly\' );
}}

Don\'t forget to put \'&\' symbol in front of errors variable.

参考链接:do_action_ref_array

结束

相关推荐

Hooks for Links Box

Possible Duplicate:Getting archive pages in WP's AJAX internal link finder? 新的有挂钩吗internal links box 创建于WP 3.1? 我正在尝试在插件中修改它。