1. Home
  2. Docs
  3. CF7 GSheetConnector PRO
  4. Custom Mail Tags

Custom Mail Tags

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:

  1. 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 );
  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;
    4 }

    In this example, “_current_url” is added as a custom mail tag.

  3. 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]";
    4 }
    5 return $output;
    6 }, 10, 3 );

    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:

custom mail tags CF7 GSheet Connector Custom Mail Tags

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;
5 }

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;
8     return $date_time;
9    }
10   }
11   return $output;
12  }, 10, 3 );

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" ) {
5   // Get all posted data here.
6   $output $posted_data['utm_source'];
7   return $output;
8   }
9  return $output;
10 }, 10, 3 );

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 );
2
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_dataarray'cf7-serial-number-5' => $serial_number ) );
7 return $posted_data;
8 }

Example 2:

1 add_filter( 'wpcf7_posted_data''replace_checkbox_values', 10, 1 );
2
3 function replace_checkbox_values( $posted_data ) {
4 $checkbox_value $posted_data['checkbox-500'];
5 unset( $posted_data['checkbox-500'] );
6 // posted data for checkbox is in array and as only single value than pass the index.
7 if$checkbox_value[0] === "Checked" ) {
8 $posted_data array_merge($posted_dataarray'checkbox-500' => 'checked' ) );
9 }
10 else {
11 $posted_data array_merge($posted_dataarray'checkbox-500' => 'unchecked' ) );
12 }
13 return $posted_data;
14 }

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();
5          //error_log("posted data: " . print_r($posted_data, true) );
6          $serial_number $posted_data['serial-number'];
7          // Make sure the field name is correct
8             return $serial_number;
9          }
10       return $output;
11    }, 10, 3 );

How can we help?