Last active
May 27, 2023 10:49
-
-
Save RadGH/d08a7466b097dfb895ec6dede2e474f5 to your computer and use it in GitHub Desktop.
Get checked checkboxes from Gravity Forms checkbox field, as array
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
<?php | |
// 1/3) Usage: | |
$entry_id = 100; // usually provided in a filter, if so you can remove this line. | |
$entry = GFAPI::get_entry( $entry_id ); // get the entry, if this was provided in a filter you can remove this line. | |
$field_id = 35; // field ID can be found when field is selected while editing the form | |
$value = rs_gf_get_checked_boxes( $entry, $field_id ); | |
// 2/3) Example output: | |
/* | |
var_dump($value); | |
array(2) { | |
["35.4"]=> | |
string(12) "Nonprofit Organizations" | |
["35.11"]=> | |
string(5) "Other" | |
} | |
*/ | |
// 3/3) Function to copy: | |
/** | |
* Return an array of checkboxes that have been checked on a Gravity Form entry. | |
* Field keys are the input ID. For example "35.4", which means the 4th item of field #35. | |
* | |
* @param $entry | |
* @param $field_id | |
* | |
* @return array | |
*/ | |
function rs_gf_get_checked_boxes( $entry, $field_id ) { | |
$items = array(); | |
$field_keys = array_keys( $entry ); | |
// Loop through every field of the entry in search of each checkbox belonging to this $field_id | |
foreach ( $field_keys as $input_id ) { | |
// Individual checkbox fields such as "14.1" belongs to field int(14) | |
if ( is_numeric( $input_id ) && absint( $input_id ) == $field_id ) { | |
$value = rgar( $entry, $input_id ); | |
// If checked, $value will be the value from the checkbox (not the label, though sometimes they are the same) | |
// If unchecked, $value will be an empty string | |
if ( "" !== $value ) $items[ $input_id ] = $value; | |
} | |
} | |
return $items; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment