Custom Mail Tags – Pro Version

Custom Mail Tags – Pro Version

Custom Mail Tags for CF7 Google Sheet Connector #

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.

    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.

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

    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.

    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 ); 

    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 #

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

Passing Values to the Google Sheet: #

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 );

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/

add_filter( "wpcf7_special_mail_tags", function( $output, $name, $html ) {
$submission = WPCF7_Submission::get_instance();
$posted_data = $submission->get_posted_data();
if ( $name === "_utm_source" ) {
  // Get all posted data here.
  $output = $posted_data['utm_source'];
  return $output;
  }
 return $output;
}, 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: #

add_filter( 'wpcf7_posted_data', 'unset_serial_number_to_posted_data', 10, 1 );

function unset_serial_number_to_posted_data( $posted_data ) {
$serial_number = $posted_data['Serial Number'];
unset( $posted_data['Serial Number'] );
$posted_data = array_merge($posted_data, array( 'cf7-serial-number-5' => $serial_number ) );
return $posted_data;
}

Example 2: #

add_filter( 'wpcf7_posted_data', 'replace_checkbox_values', 10, 1 );

function replace_checkbox_values( $posted_data ) {
$checkbox_value = $posted_data['checkbox-500'];
unset( $posted_data['checkbox-500'] );
// posted data for checkbox is in array and as only single value than pass the index.
if( $checkbox_value[0] === "Checked" ) {
$posted_data = array_merge($posted_data, array( 'checkbox-500' => 'checked' ) );
}
else {
$posted_data = array_merge($posted_data, array( 'checkbox-500' => 'unchecked' ) );
}
return $posted_data;
}

Example 3:

add_filter( "wpcf7_special_mail_tags", function( $output, $name, $html ) {
      if ( $name === "_cf7_serial_number" ) {
         $submission = WPCF7_Submission::get_instance();
         $posted_data = $submission->get_posted_data();
         //error_log("posted data: " . print_r($posted_data, true) );
         $serial_number = $posted_data['serial-number']; 
         // Make sure the field name is correct
            return $serial_number;
         }
      return $output;
   }, 10, 3 );

 

Go to Top