1

I'm having real trouble working on a WP plugin because I can't seem to access my PHP data to see what's going on.

The plugin makes an ajax call which hangs if I put anything it doesn't like in my PHP function (below).

Instead of the line

do_action_ref_array( 'bookly_validate_custom_field', array( $field, &$this->errors, $fields[ $field->id ] ) );

I've tried

$this->errors['custom_fields'][ $cart_key ][ $field->id ] = __( 'bad', 'bookly' );

Which works and displays an error message of "bad" on the page (which is like what I want, although I want to make it conditional).

However, if I put var_dump($this) in there, the ajax call hangs.

Also echo '<script>console.log("My")</script>'; didn't get me anywhere

Eventually I'd like to write a function to tie into the hook from outside, but I need to play with the code here to work out how to do that, and since I can't see what's going on I'm stuck.

Any help much appreciated.

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 \stdClass
                     */
                    do_action_ref_array( 'bookly_validate_custom_field', array( $field, &$this->errors, $fields[ $field->id ] ) );
                }
            }
        }
    }
2

If you want to see your object in that variable you have to convert it to a string:

$data_str = print_r( $this, true );

Then you can include it on your error array, or if you're just debugging your code you can send it to your web server's error log file using error_log:

error_log( $data_str );
  • That was very helpful, thank you - at least now I have some access to the php variables/objects. Seems like an odd way of having to debug though. I'm sending errors to the wp debug.log file. I'm thinking there must be a better way to see what's going on. Maybe I need to learn proper debugging with xdebug or something. – Robin Jul 10 '17 at 19:53
  • Yes, just search for php xdebug and your editor of choice and you'll certainly find an extension to use it. Debugging with breakpoints Is certainly a better way to do it. – dbeja Jul 10 '17 at 20:32

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.