Created
May 14, 2018 12:08
-
-
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)
This file contains hidden or 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
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