Skip to content

Instantly share code, notes, and snippets.

@hisnipes
Last active December 10, 2021 14:03
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hisnipes/6603201 to your computer and use it in GitHub Desktop.
Save hisnipes/6603201 to your computer and use it in GitHub Desktop.
Wordpress GravityForms Google Spreadsheet Hook - dynamically populate radio button choices using data from a Google Spreadsheet (should allow for easier collaboration with non-tech colleagues). Matches with today's date to pull relevant information from the table. Thanks to Pamela Fox (parsing JSON output with PHP: https://gist.github.com/pamela…
<?
// Gravity Form Hook to SQL Database for Today's Dishes //
// the ### in gform_pre_render_### locates the id of your form //
add_filter('gform_pre_render_5', 'todays_dishes');
function todays_dishes($form){
foreach($form['fields'] as &$field){
// look for fields with css class todays_dishes and only target those
if($field['type'] != 'radio' || strpos($field['cssClass'], 'todays_dishes') === false)
continue;
// Hook into Google Spreadsheets //
$url = 'http://spreadsheets.google.com/feeds/list/[INSERT YOUR SHEET KEY HERE]/od6/public/values?alt=json';
$file = file_get_contents($url);
$json = json_decode($file);
$rows = $json->{'feed'}->{'entry'};
// Get the current date //
date_default_timezone_set('America/Los_Angeles');
$todays_date = date("Y-m-d");
$dishes = array();
foreach($rows as $row) {
$date = $row->{'gsx$date'}->{'$t'};
$dish = $row->{'gsx$dish'}->{'$t'};
if ($date === $todays_date) {
$dishes[] = $dish;
}
}
// this controls how the selected statements are displayed as radio button values
foreach($dishes as $single_dish){
$choices[] = array('text' => $single_dish, 'value' => $single_dish );
}
$field['choices'] = $choices;
}
// once complete, show the form
return $form;
}
?>
@domes2
Copy link

domes2 commented Aug 16, 2020

Ok, I this is perfect for what I want to use to populate a field dynamically with gravity forms for a daily inventory sheet. I have zero idea how to implement this. Can anyone explain how this code works in the forms. Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment