Dynamically Add Custom Taxonomy as Check Box Options in Gravity Forms

One of the things that makes workflows work is the removal of manual work. In this case, we want to make a simple way to track software updates. We’re using Gravity Forms to do this and we wanted to categorize the update types in a way that would help us see what was addressed by the update so we could align it to things like accessibility, security, privacy, etc. It’d also enable us to show progress in those areas pretty simply.

I created a custom taxonomy for this which I called update-types. That way there’s only one place to manage these terms. I wanted any changes in this taxonomy to be automatically reflected in the Gravity Form field. That way there’s just one place to manage things.

The following script ties whatever we put in that taxonomy to a checkbox field in Gravity Forms.

The following example is modified from the example Gravity Forms gives you.

// NOTE: update the '1' to the ID of your form
add_filter( 'gform_pre_render_1', 'dlinq_update_populate_type' );
add_filter( 'gform_pre_validation_1', 'dlinq_update_populate_type' );
add_filter( 'gform_pre_submission_filter_1', 'dlinq_update_populate_type' );
add_filter( 'gform_admin_pre_render_1', 'dlinq_update_populate_type' );
function dlinq_update_populate_type( $form ) {
 
    foreach( $form['fields'] as &$field )  {
 
        //NOTE: replace 5 with your checkbox field id
        $field_id = 4;
        //NOTE: replace 'update-types' with your custom taxonomy name
        $custom_taxonomy = 'update-types';
        if ( $field->id != $field_id ) {
            continue;
        }
 
        $terms = get_terms( array(
		        'taxonomy' =>  $custom_taxonomy,
		        'hide_empty' => false,
		        'orderby'   =>'title',
		        'order'   =>'ASC',
		    ) ); 
        $input_id = 1;
        foreach( $terms as $term ) {
 
            //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
            if ( $input_id % 10 == 0 ) {
                $input_id++;
            }
 
            $choices[] = array( 'text' => $term->name, 'value' => $term->name);
            $inputs[] = array( 'label' => $term->name, 'id' => "{$field_id}.{$input_id}");
 
            $input_id++;
        }
 
        $field->choices = $choices;
        $field->inputs = $inputs;
 
    }
 
    return $form;
}

Comments on this post

No comments.

Leave a Reply

Trackbacks and Pingbacks on this post

No trackbacks.

TrackBack URL