Search code examples
wordpresscontact-form-7

Invalid JSON error when using var_dump or echo in functions.php


i have a customized Logic for my CF7 Form with the Hook before_mail_sent, whoever when i try to dump my Form Data like below i get this error in the Console on submit:

{code: 'invalid_json', message: 'The response is not a valid JSON response.'}

The Mail is sent but missing some data, thats why i want to print for debugging. A rollback to previous CF7 Version where it worked changed nothing.

2 weeks ago i was debugging the code and all was working fine. Im a bit stuck with the error message, hope somebody can help and explain.

Heres my code in functions.php:

   add_action('wpcf7_mail_sent', 'ftw_veranstaltungsForm_afterSent' );
    function ftw_veranstaltungsForm_afterSent( $contact_form ){
        
        // to get form id
        $form_id = $contact_form->id();
        
        // Formular daten -> Assoziatives Array
        $submission = WPCF7_Submission::get_instance();
        $posted_data = $submission->get_posted_data();
        
        var_dump($posted_data);
        die;
}

Thanks in advance :)


Solution

  • It seems the action is being called in the middle of AJAX request and var_dump() messes up the JSON response.

    you need to replace

    var_dump($posted_data);
    

    with

    error_log( print_r( $posted_data, true ) );
    

    and remove die();

    and check the logged data in debug.log file.

    https://wordpress.org/support/article/debugging-in-wordpress/