Created
April 18, 2016 23:10
-
-
Save feliciaceballos/42f452919ee85b0cdb86fcf9adc12f1f to your computer and use it in GitHub Desktop.
Dynamically populate drop down in Gravity Forms with list of users with author role
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
//replace 72 below with your form id number | |
add_filter( 'gform_pre_render_72', 'populate_posts' ); | |
add_filter( 'gform_pre_validation_72', 'populate_posts' ); | |
add_filter( 'gform_pre_submission_filter_72', 'populate_posts' ); | |
add_filter( 'gform_admin_pre_render_72', 'populate_posts' ); | |
function populate_posts( $form ) { | |
foreach ( $form['fields'] as &$field ) { | |
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) { | |
continue; | |
} | |
// You can change authors to another role. You'd change that here | |
$users = get_users( 'role=author' ); | |
$choices = array(); | |
foreach ( $users as $user ) { | |
$choices[] = array( 'text' => $user->display_name, 'value' => $user->display_name ); | |
} | |
// update 'Select a Post' to whatever you'd like the instructive option to be | |
$field->placeholder = 'Select a sales rep'; | |
$field->choices = $choices; | |
} | |
return $form; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment