Skip to content

Instantly share code, notes, and snippets.

@karlazz
Created June 25, 2014 22:26
Show Gist options
  • Select an option

  • Save karlazz/4a9feba239d548d54bb5 to your computer and use it in GitHub Desktop.

Select an option

Save karlazz/4a9feba239d548d54bb5 to your computer and use it in GitHub Desktop.
Read from a file into a multi-selector field in gravity forms
/* 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