Created
June 25, 2014 22:26
-
-
Save karlazz/4a9feba239d548d54bb5 to your computer and use it in GitHub Desktop.
Read from a file into a multi-selector field in gravity forms
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
| /* http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields | |
| */ | |
| add_filter('gform_pre_render_5', 'populate_posts'); | |
| function populate_posts($form){ | |
| foreach($form['fields'] as &$field){ | |
| if( strpos($field['cssClass'], 'multi-selector') === false) /* original had a check for $field['type'] != 'select' */ | |
| continue; | |
| // you can add additional parameters here to alter the posts that are retreieved | |
| // more info: http://codex.wordpress.org/Template_Tags/get_posts | |
| //$posts = get_posts('numberposts=-1&post_status=publish'); | |
| $file = fopen('sites.csv', 'r'); | |
| /* | |
| while (($line = fgetcsv($file)) !== FALSE) { | |
| //$line is an array of the csv elements | |
| print_r($line); | |
| } | |
| */ | |
| $line = fgetcsv($file); | |
| fclose($file); | |
| $posts=$line; | |
| // update 'Select a Post' to whatever you'd like the instructive option to be | |
| $choices = array(array('text' => 'Select a Site', 'value' => ' ')); | |
| foreach($posts as $post){ | |
| // $choices[] = array('text' => $post->post_title, 'value' => $post->post_title); | |
| $choices[] = array('text' => $post, 'value'=>$post); | |
| } | |
| $field['choices'] = $choices; | |
| } | |
| return $form; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment