Skip to content

Commit 072a5c4

Browse files
authored
gppa-preserve-selections.php: Fixed an issue with preserve selections not working correctly for checkbox field.
1 parent dc5b10d commit 072a5c4

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

gp-populate-anything/gppa-preserve-selections.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,26 @@
55
*/
66
// Update "123" to your form ID and "4" to your field ID.
77
add_filter( 'gppa_input_choices_123_4', function( $choices, $field, $objects ) {
8-
$selected_values = (array) rgar( gp_populate_anything()->get_field_values_from_request(), $field->id );
8+
$field_values = gp_populate_anything()->get_field_values_from_request();
9+
$selected_values = (array) rgar( $field_values, $field->id );
10+
if ( $field->type == 'checkbox' && ! empty( $selected_values ) ) {
11+
// look in field_values for values of index like 5.1, 5.2 etc. where 5 is the field id
12+
$selected_values = array();
13+
foreach ( $field_values as $key => $value ) {
14+
if ( strpos( (string) $key, $field->id . '.' ) === 0 ) {
15+
$selected_values[ $key ] = $value;
16+
}
17+
}
18+
}
19+
920
foreach ( $choices as &$choice ) {
1021
if ( in_array( $choice['value'], $selected_values, true ) ) {
1122
$choice['isSelected'] = true;
1223
}
1324
}
25+
1426
return $choices;
1527
}, 10, 3 );
28+
29+
// Update "123" to your form ID and "4" to your field ID.
30+
add_filter( 'gppa_field_choices_posted_value_123_4', '__return_false' );

0 commit comments

Comments
 (0)