Skip to content

Instantly share code, notes, and snippets.

@geekontheroad
Last active January 22, 2022 03:10
Show Gist options
  • Save geekontheroad/ea4b54cec355e9fa9a458019b9cd2393 to your computer and use it in GitHub Desktop.
Save geekontheroad/ea4b54cec355e9fa9a458019b9cd2393 to your computer and use it in GitHub Desktop.
This script will pre populate all fields with their field ID
<?php
/**
* This function will prefill every field with its Field ID number
* Email, address and phone fields are also supported
* Radio buttons will select its first option by default.
*
* Change the number 8 below to the number of your form. eg: gform_pre_render_FORMID
*
*@author Johan De Reiziger
*@link https://geekontheroad.com
*
**/
add_filter( 'gform_pre_render_8', 'auto_fill_form' );
function auto_fill_form($form) {
$fields = $form["fields"];
foreach($form["fields"] as $key => $field) {
if($field["type"] == "text") {
$field["defaultValue"] = $field["id"];
} else if($field["type"] == "textarea") {
$field["defaultValue"] = $field["id"] . " textarea";
} else if($field["type"] == "email") {
$field["defaultValue"] = $field["id"]."@example.com";
} else if($field["type"] == "radio") {
$first = true;
$choices = array();
foreach ($field["choices"] as $key => $choice) {
if ($first == true) {
$choices[] = array("text"=>$choice["text"], "value"=>$choice["value"], "isSelected"=>true);
$first = false;
} else {
$choices[] = array("text"=>$choice["text"], "value"=>$choice["value"], "isSelected"=>false);
}
}
$field["choices"] = $choices;
} else if ($field["type"] == "phone") {
$field["defaultValue"] = "(513) 122-1132";
} else if ($field["type"] == "address") {
$inputs = array();
foreach ($field["inputs"] as $key => $details) {
//error_log( print_r($details, TRUE) );
if($details["label"] == "State / Province") {
$details["defaultValue"] = "Alabama";
$inputs[] = $details;
continue;
}
$details["defaultValue"] = $field["id"] . $details["label"];
$inputs[] = $details;
}
$field["inputs"] = $inputs;
}
}
return $form;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment