Dropdown list of users (again)
-
Hi,
My last (resolved) post on this was several months ago – https://www.ads-software.com/support/topic/select-list-of-users/ -but I have now run into a problem where it works on one site but not on another, although the code has been very slightly tweaked on the site where it’s not working. This was custom code generously provided by you, so I understand will not be a priority, but I would greatly appreciate any help. To assist I will provide the code that isn’t working, and highlight the difference between that and the working code.
NOT WORKING CODE (IN MU-PLUGINS FOLDER) <?php add_filter( 'forminator_cform_render_fields', function ( $wrappers, $model_id ) { // Form id where this code will be applied if ( $model_id != 2280 || $model_id != 2300) { return $wrappers; } $user_id = get_current_user_id(); $members = friends_get_friend_user_ids( $user_id ); if ( ! empty( $members ) ) { $new_options = array(); $names = array(); foreach( $members['members'] as $member ) { $new_options[] = array( 'label' => $member->user_login, 'value' => $member->user_login, 'limit' => '', 'key' => forminator_unique_key(), ); } $names['options'] = $new_options; } // Define select fields to be populated $select_fields_data = array( 'select-1' => 'users', ); // Look for individual fields or fields in groups foreach ( $wrappers as $wrapper_key => $wrapper ) { if ( ! isset( $wrapper[ 'fields' ] ) ) { continue; } for ($index = 0; $index < count($wrapper['fields']); $index++) { if ( isset( $select_fields_data[ $wrapper[ 'fields' ][ $index ][ 'element_id' ] ] ) && ! empty( $select_fields_data[ $wrapper[ 'fields' ][ $index ][ 'element_id' ] ] ) && $select_fields_data[ $wrapper[ 'fields' ][ $index][ 'element_id' ] ] === 'users'){ $select_field = Forminator_API::get_form_field( $model_id, $wrapper['fields'][$index]['element_id'], true ); if ( $select_field ) { Forminator_API::update_form_field( $model_id, $wrapper['fields'][$index]['element_id'], $names ); $wrappers[ $wrapper_key ][ 'fields' ][ $index ][ 'options' ] = $new_options; } } } } return $wrappers; }, 10, 2);
CODE FROM WORKING VERSION THAT IS DIFFERENT // Form id where this code will be applied if ( $model_id != 6451 ) { return $wrappers; } // Retrieve group member names $user_id = get_current_user_id(); $grp_id = bp_get_current_group_id(); $args = array( 'group_id' => $grp_id, 'exclude' => $user_id, 'exclude_admins_mods' => 0); $members = groups_get_group_members($args);
With the code that’s not working, the dropdown field is there to be clicked, but nothing drops down. When I compare the actual created forms, they are identical (other than labels and descriptions) except the one that doesn’t work has one less field (a hidden field); it was imported from the other site then duplicated and edited. What I did notice though was that the options in the select field were filled with the users from the other site, and initially I hadn’t noticed this so thought the form was working with the filter. I deleted those and that is when I realised the filter wasn’t working. I have created a completely new form to see if the import and duplication was the issue, but the behaviour is the same. I have also tried removing the if condition with the form ids. It seems very odd to me that essentially the same code can work on one site but not another. I hope you have some clue as to what is happening.
- The topic ‘Dropdown list of users (again)’ is closed to new replies.