Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feliciaceballos/42f452919ee85b0cdb86fcf9adc12f1f to your computer and use it in GitHub Desktop.
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
//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