Debugging PHP object during Ajax call in WordPress

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

1 Answer
1

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

Leave a Comment