0

I've managed to get the behaviour I want by modifying some plugin code. However, I would like to move my modifications outside of the plugin code using the provided hook, but I can't seem make it work. The hook uses do_action_ref_array.

The situation is not helped by the fact that the only way I can find to access the php vars is by sending error messages to the debug.log file.

(like so):

function ra_func1($args) {
    $str = print_r($args, true);
    error_log($str);
}
add_action ( 'bookly_validate_custom_field', 'ra_func1', 10, 3);

Trouble is there's a mixture of objects and arrays and I'm now really stuck, having spent a lot of time trying to get this working. If anyone could provide a working callback that does what I want I would be very grateful.

Original plugin code:

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 ] ) );
                }
            }
        }
    }

Code with the beginnings of the functionality I want to add via an external callback:

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' );                      
                                         }
                }
            }
        }
    }
1

That feature exists because I bugged them to implement it :)

Using it confused me as well but somebody finally replied to this just the other day with this snippet:

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 );
0
0

You need to write custom function in functions.php with name of bookly_validate_custom_field and pass 5 arguments to that function

For example

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.

Reference link: do_action_ref_array

2
  • Unfortunately this doesn't work. Also, it goes against my understanding of how hooks function in WP. I thought the function name could be anything, but is hooked in by use of something like add_action ( 'bookly_validate_custom_field', 'ra_func1', 10, 4); No?. Any further ideas? Jul 10 '17 at 21:55
  • Did to add add_action statement in your code.. add_action( 'bookly_validate_custom_field','bookly_validate_custom_field',10,5)b Jul 11 '17 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.