php – How can I save a user’s Contact Form 7 inputs as separate columns in a custom WordPress table?

Question

I’m new to WordPress, PHP, and the whole lot. I’ve done some googling and found the code below, but it’s not saving the submission as I need.

I have a form that captures: first-name, last-name, checkin-date, weight, and energy-level. After the user hits submit, I want that information stored into wpdg_checkin_forms. I have the table created with the appropriate columns.

I’ve verified that the ids for the fields on the CF7 form match the IDs in the code below. I’m putting the code into the function.php file.

Thank you in advance!

Here is the code I found and how I tried to modify:

function contactform7_before_send_mail( $form_to_DB ) {
//set your db details
$mydb = new wpdb('root','','wpdg_checkin_forms','localhost');

$form_to_DB = WPCF7_Submission::get_instance();
if ( $form_to_DB ) 
    $formData = $form_to_DB->get_posted_data();
$firstname = $formData['first-name']
$lastname = $formData['last-name']
$checkindate = $formData['checkin-date']
$weight = $formData['weight']
$energylevel = $formData['energy-level'];

$mydb->insert( 'wpdg_checkin_forms', array( 'first-name' =>$firstname, 'last-name' =>$lastname, 'checkin-date' =>$checkindate, 'weight' =>$weight, 'energy-level' =>$energylevel ), array( '%s' ) );
 } remove_all_filters ('wpcf7_before_send_mail');
 add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );

0
Josh 3 weeks 2022-09-03T14:07:57-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse