Last active
December 10, 2021 14:03
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
// 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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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