In this post, you’ll learn how to connect Contact Form 7 to Google Sheets completely free.
We’ll start by adding a custom code to your website. Then, in Google Sheets We create a new script in the Apps Script section to establish the connection with WordPress.
Why Save Contact Form 7 Data to Google Sheets?
Contact Form 7 is one of the most popular form plugins for WordPress, However, it lacks built-in functionality to save and share form submissions By saving your form data to Google Sheets, you can:
- Data Archive: Saving forms in Google Sheets creates a searchable, permanent record of all submissions. This allows easy access and review of past entries whenever needed.
- Easily Share Data: Share the Google Sheet with team members for seamless collaboration.
- Automate Workflows: Use Google Sheets to trigger other processes, such as sending notifications or updating databases.
How to Save Contact Form 7 Data to Google Sheets for Free
Follow these steps to integrate Contact Form 7 with Google Sheets.
Step 1: Set Up Google Apps Script
Open a new Google Sheets document.
Click on Extensions > Apps Script.
Delete any existing code and paste the following script
function doPost(e) {
var sheetName = "Email log";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
// Create sheet if it doesn't exist
if (!sheet) {
sheet = ss.insertSheet(sheetName);
sheet.appendRow(["email", "date", "time"]);
}
// Parse the incoming request
var data = JSON.parse(e.postData.contents);
var emailBody = data.email_body;
var date = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd");
var time = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "HH:mm:ss");
// Append the data to the sheet
sheet.appendRow([emailBody, date, time]);
return "ok";
}
Save the script.
Click on Deploy > New deployment.
Under “Select type,” choose “Web app“.
Provide a description and set Execute as to Me.
Set Who has access to Anyone.
Click Deploy.
Next click the Authorize access button and complete the Authorize steps.
Copy the provided URL after deployment.
Step 2: Send CF7 Data to Google Sheets
Add the following code to your WordPress theme’s functions.php file.
You must create a child theme before making any changes to functions.php
file. Otherwise, the applied changes will be lost after each update.
Create child theme in WordPress step by step [without plugin]
As an alternative method, you can use the Code Snippets plugin to insert your codes into WordPress.
/**
* Save Contact Form 7 Data to Google Sheets free
* https://redpishi.com/wordpress-tutorials/cf7-to-google-sheets/
*/
function send_cf7_email_to_api($contact_form) {
/***********************/
$google_apps_script_url = 'YOUR_APPS_SCRIPT_URL';
/***********************/
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$posted_data = $submission->get_posted_data();
$clean_body = implode("\n", $posted_data);
$data = array('email_body' => $clean_body);
$response = wp_remote_post($google_apps_script_url, array(
'method' => 'POST',
'body' => wp_json_encode($data),
'headers' => array(
'Content-Type' => 'application/json',
),
));
if (is_wp_error($response)) {
error_log('CF7 Email API Error: ' . $response->get_error_message());
}
}
}
add_action('wpcf7_before_send_mail', 'send_cf7_email_to_api');
Replace YOUR_APPS_SCRIPT_URL with the URL of your deployed Google Apps Script from Step1.
By following these steps, you can seamlessly save your Contact Form 7 submissions to Google Sheets for free.
If this article is difficult for you to read in text, you can watch the video version below.
Can\’t I do this for a specific contact form of contact form 7?