Populate Choice Label

When mapping a choice-based field to a Single Line Text field, you may want to populate the choice label rather than the choice text. This snippet will handle this automatically.

Code

Filename: gpep-populate-choice-label.php

<?php
/**
 * Gravity Perks // Easy Passthrough // Populate Choice Label
 * https://gravitywiz.com/documentation/gravity-forms-easy-passthrough/
 *
 * When mapping a choice-based field to a Single Line Text field, you may want to populate the choice label rather than
 * the choice text. This snippet will handle this automatically.
 */
add_filter( 'gpep_target_field_value', function( $value, $form_id, $target_field_id, $source_field ) {

	$target_field = GFAPI::get_field( $form_id, $target_field_id );
	if ( $target_field->get_input_type() !== 'text' ) {
		return $value;
	}

	if ( empty( $source_field->choices ) ) {
		return $value;
	}

	$choice = $source_field->get_selected_choice( $value );
	$value  = $choice['text'];

	return $value;
}, 10, 4 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.