Last active
December 10, 2021 14:25
-
-
Save alex-authlab/e0130162dc4307f6759a7d3a5a477363 to your computer and use it in GitHub Desktop.
Populate WP fluent form dropdown from Google sheet
This file contains hidden or 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
| add_filter('fluenform_rendering_field_data_select', function ($data, $form) { | |
| if ($form->id != 3) { | |
| return $data; | |
| } | |
| // check if the name attriibute is 'dropdown' , it is by default dropdown for the first dropdown input | |
| if (\FluentForm\Framework\Helpers\ArrayHelper::get($data, 'attributes.name') != 'dropdown') { | |
| return $data; | |
| } | |
| //google sheet shared and published CSV link | |
| $csvUrl = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vT5FV_MYPwTqjhelf6_g02pS1Y60DbdkHDWPvRKVzLPeZSKFNFa4y6kMas3amvM7v1El-b1PVdC0wrP/pub?output=csv'; | |
| $columName = 'Players'; // 'Players' is the column name | |
| $uniqueData = true; // remove duplicate values | |
| $result=[] ; | |
| if (!class_exists('CSVParser')) { | |
| require_once(FLUENTFORMPRO_DIR_PATH . 'libs/CSVParser/CSVParser.php'); | |
| } | |
| $csvParser = new \CSVParser; | |
| $content = file_get_contents($csvUrl); | |
| $csvParser->load_data($content); | |
| $rows = $csvParser->parse($csvParser->find_delimiter()); | |
| if(!$rows) { | |
| return $data; | |
| } | |
| $headers = array_shift($rows); // remove the first item | |
| $headerIndex = array_search($columName, $headers); | |
| foreach ($rows as $row) { | |
| if(!empty($row[$headerIndex])) { | |
| $result[]= | |
| [ | |
| "label" => $row[$headerIndex], | |
| "value" => $row[$headerIndex], | |
| "calc_value" => "" | |
| ]; | |
| } | |
| } | |
| $result = ($uniqueData === true) ? array_map("unserialize", array_unique(array_map("serialize", $result))) : $result; | |
| // We are merging with existing options here | |
| $data['settings']['advanced_options'] = array_merge($data['settings']['advanced_options'], $result ); | |
| return $data; | |
| }, 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment