There are numerous plugins available to save and view the cf7 submission in the dashboard. If you want ignore the plugin and write your own code to save the cf7 then follow this article.
I am using acf fields to store the data submitted from cf7.
1. First, create an acf options page. I am creating Cf7 Submissions as my option page where I will list all my form submissions values
1 2 3 4 5 6 7 |
if( function_exists('acf_add_options_page') ) { acf_add_options_page(array( 'page_title' => 'Cf7 Submissions', 'menu_title' => 'Cf7 Submissions', 'redirect' => false )); } |
2. Create a form using CF7. I am using the default form cf7 provides
3. Create a repeater field in acf to save the form submitted values in the database and assign it to Cf7 Submissions options page
4. CF7 provides hook wpcf7_before_send_mail which enable us to process the submitted data the way we like before submitting the form. I am using this hook to save the form submission in the dashboard
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
add_action("wpcf7_before_send_mail", "app_send_email", 10, 1); function app_send_email($wpcf){ // get current SUBMISSION instance $submission = WPCF7_Submission::get_instance(); if(!$submission) return $wpcf; // get submission data $data = $submission->get_posted_data(); /* * I have created the matching field name in the database, * so that I don't get confused with the form field name and database fields where I am storing my data's */ $dataToSave = [ 'your-name' => $data['your-name'], 'your-email' => $data['your-email'], 'your-subject' => $data['your-subject'], 'your-message' => $data['your-message'] ]; saveSubmissionToAcf($dataToSave); return $wpcf; } function saveSubmissionToAcf(array $newSubmission){ $oldSubmission = get_field("cf7_submission", "option"); if(!$oldSubmission) $oldSubmission = []; $formSubmittedValues = array_merge($oldSubmission, [ $newSubmission ]); update_field( "cf7_submission", $formSubmittedValues, "option" ); } |
Note : Add all the aforementioned codes in your function.php
5. update_field updates the values in the database
We are capturing the previously submitted data, so that we could merge it with the new submitted form values .
Note : The previously saved data’s in the repeater fields are lost if we don’t merge and update it with the new data’s submitted by the form
6. Check the cf7 submission options page to see the lists of saved submitted form fields in the dashboard
Hits: 62