Trying to get contact form 7 post data to debug to screen

Viewed 5250

I've been trying to get contact form 7 post data to debug the form submission so that I can use it for a plugin I'm trying to work on. However, when I use var_dump or print_r, I can't get the data anywhere.

I've started with this.

add_action( 'wpcf7_before_send_mail', 'my_process_cf7_form_data' );
function my_process_cf7_form_data() {

    $submission = WPCF7_Submission::get_instance();
        if ( $submission ) {
            $posted_data = $submission->get_posted_data();    
    }
    var_dump($posted_data);
}

But I'm not getting any output.

2 Answers

You can't just dump this data to the screen, because it's part of an ajax function. You can however dump it to the error log and tail it in bash, or view the output of the log with FTP.

If you do this instead:

add_action( 'wpcf7_before_send_mail', 'my_process_cf7_form_data' );
function my_process_cf7_form_data() {

    $submission = WPCF7_Submission::get_instance();
        if ( $submission ) {
            $posted_data = $submission->get_posted_data();    
    }

    ob_start();
    var_dump($posted_data);
    error_log(ob_get_clean());

}

then either view your php_error_log for this domain, or if you have wp-debug enabled and error logging to file (in your wp-config.php).

define( 'WP_DEBUG',         true );
define( 'WP_DEBUG_LOG',     true );

then you can view the debug.log in wp-content folder.

I managed to debug by altering the mail content and sending it to myself, as described here: How to change data before sending in Contact form 7?

add_action('wpcf7_before_send_mail', 'w2p_on_submit', 10, 3);
function w2p_on_submit( $form, &$abort, $submission )
{
  $debug="1";
  $mail = $form->prop( 'mail' );
  $new_mail = str_replace( '[your-name]', $debug, $mail );
  $form->set_properties( array( 'mail' => $new_mail ) );
  return $form;
}

(I couldn't get the log to work. )

Related