Autofill field with Cookies & Query String

I am using the code below to autofill the form using cookies. However, this code does not allow me to capture query string data. Ex. I am able to fetch name number email but not utm_source or utm_medium after adding this code to functions.php

I am not a coder. Can someone help me modify the code so I can capture both

  1. Field values via cookies
  2. UTM parameters via a query string
/* ----- create cookies to remember items for auction gravity forms       ----- */ 
add_action("gform_pre_submission", "pre_submission_handler");
function pre_submission_handler($form_meta) {
  $saveVars = array("first", "last", "insta", "email");
  foreach($form_meta["fields"] as $field) {
    if( $field["allowsPrepopulate"] ){
      if( is_array($field["inputs"]) ){
        foreach($field["inputs"] as $sub){
          $val = $_POST["input_" . str_replace(".", "_", $sub["id"])];
          setcookie("gf_".$sub["name"], $val, time() + 31536000, COOKIEPATH, COOKIE_DOMAIN, false, true);
      }
	  }else{
        $val = $_POST["input_" . $field["id"]];
        setcookie("gf_".$field["inputName"], $val, time() + 31536000, COOKIEPATH, COOKIE_DOMAIN, false, true);
      }
    }
  }
}
 
add_filter("gform_pre_render", "add_auto_update_filters");
$contego_callbacks = array();
function add_auto_update_filters($form){
  foreach($form["fields"] as &$field){
    if( $field["allowsPrepopulate"] ){
      if( is_array($field["inputs"]) ){
        foreach($field["inputs"] as $sub){
          $fieldName = $sub["name"];
          add_filter("gform_field_value_" . $fieldName, function($f) use ($fieldName){
            return $_COOKIE["gf_" . $fieldName];
          });
        }
      }else{
        $fieldName = $field["inputName"];
        add_filter("gform_field_value_" . $fieldName, function($f) use ($fieldName){
          return $_COOKIE["gf_" . $fieldName];
        });
      }
 
    }
  }
  return $form;
}```

Hi,

It’s pretty simple to get parameters from a query string. Just google something like ‘php get query parameters from url’

You’ll need to add the code to your existing functions to first grab the URL params and then do something with them.

Simon

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.