Create Custom Select field with all users as Label/Value using wpt_fields_options

Use at your own discretion

The code requires a Select Field with Title Select a User and one option set.
It then completes the options of the Select Field with User Login Names as labels and User IDs as values.

Cannot be used for Views Custom Searches.

add_filter( 'wpt_field_options', function ( $current_options, $title_of_field ) {
    if ( $title_of_field != 'Select a User' ) {
        // not our "Headline Color" field
        return $current_options;
    }

    $users_objects_array = get_users();
    $users = array();

    foreach ($users_objects_array as $user_object_array) {
    	$users[$user_object_array->data->user_login] = $user_object_array->ID;
    }
    //error_log(print_r($user_id_array, true));

    if ( ! $users_objects_array ) {
        // no theme colors are set yet
        return array(
            array(
                '#title' => 'No users available',
                '#value' => 0
            )
        );
    }

    $new_options = array();
    foreach ( $users as $user => $user_value ) {
        $new_options[] = array(
            '#title' => $user . '-' . $user_value,
            '#value' => $user_value
        );
    }
    

    return $new_options;
}, 30, 2 );

Let us know if this snippet is not working for you:

This snippet doesn’t work
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments