Home > Blockchain >  How can I save user input from a Contact Form 7 into a Custom Table in my Wordpress database?
How can I save user input from a Contact Form 7 into a Custom Table in my Wordpress database?

Time:09-09

I have a contact form called Check In that I want to store in the table wpxo_checkin_forms. The columns are: userid, firstname, lastname, weight, checkindate, energylevel (3 radio button options), missmeal (two radio button options), and missworkout (two radio button options).

I found this code here that I tried to modify and put in my child theme's function.php, but when I submit the form, it does not save in the wpxo_checkin_forms table.

What am I doing wrong here?

add_action( 'wpcf7_submit', 'SE_379325_forward_cf7', 10, 2 );
function SE_379325_forward_cf7( $form, $result ) {
    if ( ! class_exists( 'WPCF7_Submission' ) ) {
        return;
    }
    $submission = WPCF7_Submission::get_instance();
    if ( 'mail_sent' === $result['status'] ) { // proceed only if email has been sent.
        $posted_data = $submission->get_posted_data();
        save_posted_data( $posted_data, $form->id );
    }
}
// your insert function.
function save_posted_data( $posted_data, $form_id ) {
    if ( 1522 !== $form_id ) {
        global $wpdb;
        $wpdb->insert(
            $wpdb->prefix . 'wpxo_checkin_forms',
            array(
                'userid' => $posted_data['userid'],
                'firstname' => $posted_data['first-name'],
                'lastname' => $posted_data['last-name'],
                'checkindate' => $posted_data['checkin-date'],
                'weight' => $posted_data['weight'],
                'energylevel' => $posted_data['energy-level'],
                'missmeal' => $posted_data['miss-meal'],
                'missworkout' => $posted_data['miss-workout']
            ),
            array( '%s' )
        );
    }
}```

CodePudding user response:

Did you debug this code? First make sure that your function runs, If it is running then check the email condition. you have to stop emailing for inserting testing.

CodePudding user response:

The following code should work:

add_action( 'wpcf7_submit', 'SE_379325_forward_cf7', 10, 2 );
function SE_379325_forward_cf7( $form, $result ) {
    if ( ! class_exists( 'WPCF7_Submission' ) ) {
        return;
    }
    $submission = WPCF7_Submission::get_instance();
    if ( 'mail_sent' === $result['status'] ) { // proceed only if email has been sent.
        $posted_data = $submission->get_posted_data();
        save_posted_data( $posted_data, $form->id );
    }
}
// your insert function.
function save_posted_data( $posted_data, $form_id ) {
    if ( 1522 !== $form_id ) {
        global $wpdb;
        $wpdb->insert(
            $wpdb->prefix . 'wpxo_checkin_forms',
            array(
                'userid' => $posted_data['userid'],
                'firstname' => $posted_data['first-name'],
                'lastname' => $posted_data['last-name'],
                'checkindate' => $posted_data['checkin-date'],
                'weight' => $posted_data['weight'],
                'energylevel' => $posted_data['energy-level'],
                'missmeal' => $posted_data['miss-meal'],
                'missworkout' => $posted_data['miss-workout']
            )
        );
    }
}
  • Related