Created
May 19, 2017 14:29
-
-
Save joshuaiz/f0e581a769db1b7a29e44f8e009702ca to your computer and use it in GitHub Desktop.
Pre-populate Gravity Forms Dropdown
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
<?php | |
add_filter( 'gform_pre_render_2', 'populate_posts' ); | |
add_filter( 'gform_pre_validation_2', 'populate_posts' ); | |
add_filter( 'gform_pre_submission_filter_2', 'populate_posts' ); | |
add_filter( 'gform_admin_pre_render_2', 'populate_posts' ); | |
function populate_posts( $form ) { | |
foreach ( $form['fields'] as &$field ) { | |
if ( strpos( $field->cssClass, 'populate-posts' ) === false ) { | |
continue; | |
} | |
// you can add additional parameters here to alter the posts that are retrieved | |
// more info: [http://codex.wordpress.org/Template_Tags/get_posts](http://codex.wordpress.org/Template_Tags/get_posts) | |
$posts = get_posts( 'post_type=team&numberposts=-1&post_status=publish' ); | |
$choices = array(); | |
foreach ( $posts as $post ) { | |
$team = get_post_meta( $post->ID, 'team_members' ); | |
$leader = get_post_meta( $post->ID, 'display_name' ); | |
$leaders = "<pre>".implode(",\n",$leader)."</pre>"; | |
$separator = "<pre>".implode(",\n",$team)."</pre>"; | |
$choices[] = array( 'text' => $post->post_title . '<section>' . $leaders . $separator . '</section>', 'value' => $post->post_title, 'imageChoices_image' => get_the_post_thumbnail_url($post->ID, 'medium') ); | |
} | |
// update 'Select a Post' to whatever you'd like the instructive option to be | |
$field->placeholder = 'Select a Post'; | |
$field->choices = $choices; | |
} | |
return $form; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment