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 richerimage/052a6aa65e9cda2b5f3eabca3076c851 to your computer and use it in GitHub Desktop.
Save richerimage/052a6aa65e9cda2b5f3eabca3076c851 to your computer and use it in GitHub Desktop.
Gravity Form - Display list of Users as a dropdown (User ID is the value)
add_filter( 'gform_pre_render_FORM_ID', 'dna_user_list_dropdown' );
add_filter( 'gform_pre_validation_FORM_ID', 'dna_user_list_dropdown' );
add_filter( 'gform_pre_submission_filter_FORM_ID', 'dna_user_list_dropdown' );
add_filter( 'gform_admin_pre_render_FORM_ID', 'dna_user_list_dropdown' );
function dna_user_list_dropdown( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'post_custom_field' || strpos( $field->cssClass, 'author-list' ) === false ) {
continue;
}
$args = array(
'fields' => array( 'ID', 'display_name' ),
);
$users = get_users($args);
$choices = array();
foreach ( $users as $user ) {
$choices[] = array(
'text' => $user->display_name,
'value' => $user->ID
);
}
$field->placeholder = 'Select a User';
$field->choices = $choices;
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment