The CF7 Google Sheet Connector (CF7 GSC) allows you to create custom mail tags, to have the 3rd plugin data to be saved at the Google Sheet.
Usage:
To create custom mail tags, follow these steps:
-
Hook into the “gscf7_special_mail_tags” Filter:
In order to add custom mail tags, you need to hook into the “gscf7_special_mail_tags” filter. This filter is specific to the CF7 GSC plugin and allows you to define custom mail tags.
1 |
add_filter( "gscf7_special_mail_tags" , "add_custom_mail_tag" , 10, 2 ); |
-
Define the “add_custom_mail_tag” Function:
Create a custom function, such as “add_custom_mail_tag,” to handle the addition of custom mail tags. This function takes two parameters:$custom_mail_tags: An array where you specify the names of custom mail tags you want to create (e.g., “_current_url”).$form_id: The Contact Form 7 form ID for which you want to add the custom mail tag.
1 |
function add_custom_mail_tag( $custom_mail_tags , $form_id ) { |
2 |
$custom_mail_tags [] = "_current_url" ; |
3 |
return $custom_mail_tags ; |
In this example, “_current_url” is added as a custom mail tag.
-
Pass Values to the Google Sheet:
After creating custom mail tags, you can pass values associated with these tags to the Google Sheet. To do this, use another filter, “wpcf7_special_mail_tags,” provided by CF7. This filter intercepts and processes the custom mail tags before they are included in the email content.
1 |
add_filter( "wpcf7_special_mail_tags" , function ( $output , $name , $html ) { |
2 |
if ( $name === "_current_url" ) { |
3 |
return "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ; |
In this example, when the “_current_url” custom mail tag is encountered, it dynamically generates and returns the current URL of the web page where the form was submitted.
Output:
For the provided example, if you include the “_current_url” custom mail tag in your CF7 form email template, it will be replaced with the actual URL of the web page where the form was submitted. For instance:
html
Your submission was received from the following URL: [_current_url]
After processing, the output in the email will be:
bash
Your submission was received from the following URL: http://demo.gsheetconnector.com/wp-json/contact-form-7/v1/contact-forms/137/feedback
This allows you to dynamically include relevant information in your CF7 form emails, enhancing their content and relevance to users.
Date & Time Merge as a Custom Mail Tag
Defining Custom Mail tag
1 |
add_filter( "gscf7_special_mail_tags" , "add_custom_mail_tag" , 10, 2 ); |
2 |
function add_custom_mail_tag( $custom_mail_tags , $form_id ) { |
3 |
$custom_mail_tags [] = "_datetime" ; |
4 |
return $custom_mail_tags ; |
Passing Values to the Google Sheet:
1 |
add_filter( "wpcf7_special_mail_tags" , function ( $output , $name , $html ) { |
2 |
if ( $name === "_datetime" ) { |
3 |
$submission = WPCF7_Submission::get_instance(); |
4 |
if ( $timestamp = $submission ->get_meta( 'timestamp' ) ) { |
5 |
$date = date_i18n( get_option( 'date_format' ), $timestamp ); |
6 |
$time = date_i18n( get_option( 'time_format' ), $timestamp ); |
7 |
$date_time = $date . " " . $time ; |
Working with UTM parameters
This is also supported in the FREE version as well
But make sure, you must be using this UTM Plugin for that
example: https://wordpress.org/plugins/easy-utm-tracking-with-contact-form-7/
1 |
add_filter( "wpcf7_special_mail_tags" , function ( $output , $name , $html ) { |
2 |
$submission = WPCF7_Submission::get_instance(); |
3 |
$posted_data = $submission ->get_posted_data(); |
4 |
if ( $name === "_utm_source" ) { |
6 |
$output = $posted_data [ 'utm_source' ]; |
Modify Data Submitted by CF7 before it’s processed
This code is a WordPress filter hook used with the Contact Form 7 (CF7) plugin. It modifies the data submitted by a CF7 form before it’s processed, specifically dealing with a field called “Serial Number.”
Example 1:
1 |
add_filter( 'wpcf7_posted_data' , 'unset_serial_number_to_posted_data' , 10, 1 ); |
3 |
function unset_serial_number_to_posted_data( $posted_data ) { |
4 |
$serial_number = $posted_data [ 'Serial Number' ]; |
5 |
unset( $posted_data [ 'Serial Number' ] ); |
6 |
$posted_data = array_merge ( $posted_data , array ( 'cf7-serial-number-5' => $serial_number ) ); |
Example 2:
1 |
add_filter( 'wpcf7_posted_data' , 'replace_checkbox_values' , 10, 1 ); |
3 |
function replace_checkbox_values( $posted_data ) { |
4 |
$checkbox_value = $posted_data [ 'checkbox-500' ]; |
5 |
unset( $posted_data [ 'checkbox-500' ] ); |
7 |
if ( $checkbox_value [0] === "Checked" ) { |
8 |
$posted_data = array_merge ( $posted_data , array ( 'checkbox-500' => 'checked' ) ); |
11 |
$posted_data = array_merge ( $posted_data , array ( 'checkbox-500' => 'unchecked' ) ); |
Example 3:
1 |
add_filter( "wpcf7_special_mail_tags" , function ( $output , $name , $html ) { |
2 |
if ( $name === "_cf7_serial_number" ) { |
3 |
$submission = WPCF7_Submission::get_instance(); |
4 |
$posted_data = $submission ->get_posted_data(); |
6 |
$serial_number = $posted_data [ 'serial-number' ]; |