How to add Custom Field in Contact Form 7 Google Sheets Connector

posted in: Uncategorized | 0

Today, google sheet is a must and everyone one day need to have it. If you are a wordpress user, you difinetly have heard about one of the best form builder plugins i.e. Contact Form 7. However, contact form 7 is not very user friendly and you need add-on to expand its user freiendlies.
CR7 Googlesh Sheet Connector is one of the best plugins that you can send all you data from contact form 7 to google sheet very easily. You can read about it in many review websites.However, today I’m going to provide two hooks that you can use in this plugin to add Custom Filed as you wish.

Hooks to use Custom Mail tag:

Use to display the custom mail tags to Google Sheet Pro tab of Contact Form 7 settings and fetch the values using CF7 hook “wpcf7_special_mail_tags” to the google sheet.

add_filter( “gscf7_special_mail_tags”, “add_custom_mail_tag”, 10, 2 );

Usage

Parameters

  • $custom_mail_tags : An array to pass the custom mail tag name example “_current_url”.
  • $form_id : Contact Form 7 form id.

Example 1: Send Current URL to google sheet:

Add following to to function.php of your theme. It is recommended to use child theme to avoid loosing it after theme update.

add_filter( "gscf7_special_mail_tags", "add_custom_mail_tag", 10, 2 );
function add_custom_mail_tag( $custom_mail_tags, $form_id ) {
	$custom_mail_tags[] = "_current_url";
	return $custom_mail_tags; 
}

To pass values to the Google Sheet, use CF7 hook. To do so, add the following code exactly below the above code in function.php

add_filter("wpcf7_special_mail_tags", function( $output,  $name,  $html ) {
    if ( $name === "_current_url" ) {
      return "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    }
  	return $output;
}, 10, 3 );

Example 2: Add timestamp (datetime) tag

To combine both date and time you would be required to create a Custom mail tag. For that, We provide a hook gscf7_special_mail_tags to create a custom mail tag. You have to add the following code in functions.php file of your theme.

add_filter( "gscf7_special_mail_tags", "add_custom_mail_tag", 10, 2 );

function add_custom_mail_tag( $custom_mail_tags, $form_id ) {

   $custom_mail_tags[] = "_datetime";

   return $custom_mail_tags;
}

This will display Custom Tag  [_datetime] under Custom Mail Tags section of Google Sheet Settings.  Select that tag and click save to add this mail tag in your Google Sheet Header.
Then, use CF7 hook wpcf7_special_mail_tags to return the value of this custom mail tag. Just add following code below above code in functions.php file. This will take the current date and time when the form is submitted.

add_filter( "wpcf7_special_mail_tags", function( $output, $name, $html ) {
   if ( $name === "_datetime" ) {
      $submission = WPCF7_Submission::get_instance();
      if ( $timestamp = $submission->get_meta( 'timestamp' ) ) {
         $date = date_i18n( get_option( 'date_format' ), $timestamp );
         $time = date_i18n( get_option( 'time_format' ), $timestamp );
         $date_time = $date . " " . $time;
         return $date_time;
      }
   }
   return $output;
}, 10, 3 );

Important Note:

1) The Date and Time taken is as per WordPress general settings. You can modify date and time format from admin panel Settings -> General or by using date function.

2) When you copy code from email to functions.php file make sure about inverted commas not get replaced to ( “ ” ) format.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.