spivurno
7/22/2014 - 5:01 PM

Gravity Wiz // Gravity Forms // Get Custom Choices Helper Function

Gravity Wiz // Gravity Forms // Get Custom Choices Helper Function

<?php
/**
* Gravity Wiz // Gravity Forms // Get Custom Choices Helper Function
*
* A function that accepts a group name of one of your Gravity Form "Custom Choices" (http://grab.by/yNaS) and
* returns an array with the text and value options for each choice.
*
* @author    David Smith <david@gravitywiz.com>
* @license   GPL-2.0+
* @link      http://gravitywiz.com/...
*/
function gw_get_custom_choices( $group ) {

    $custom_choices = GFFormsModel::get_custom_choices();
    $choices = array();

    foreach( $custom_choices as $group_name => $choice_group ) {

        if( $group_name != $group ) {
            continue;
        }

        foreach( $choice_group as $choice ) {

            list( $text, $value ) = array_pad( explode( '|', $choice ), 2, false );

            $choice = array(
                'text' => $text,
                'value' => $value ? $value : $text
            );

            $choices[] = $choice;

        }
    }

    return $choices;
}

## Usage Example

$colors = gw_get_custom_choices( 'Colors' );

?>

<ul>

<?php foreach( $colors as $color ): ?>

    <li><?php echo $color['text']; ?></li>

<?php endforeach; ?>

</ul>