Use Advanced Custom Field’s Choice Label

By default, Populate Anything will populate the value of any choice-based ACF custom field that is mapped. This snippet allows you to populate the label of an ACF choice (e.g. Radio Button, Checkbox, etc) instead.

Instructions

  1. Install the snippet. https://gravitywiz.com/documentation/how-do-i-install-a-snippet/

  2. Add the CSS class “gppa-use-acf-choice-label” to the CSS Class Name field setting for any field that should use the ACF choice label.

Code

Filename: gppa-use-acf-choice-label.php

<?php
/**
 * Gravity Perks // Populate Anything // Use Advanced Custom Field's Choice Label
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * By default, Populate Anything will populate the value of any choice-based ACF custom field that is mapped. This snippet
 * allows you to populate the label of an ACF choice (e.g. Radio Button, Checkbox, etc) instead.
 *
 * Instructions
 *
 * 1. Install the snippet.
 *    https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 *
 * 2. Add the CSS class "gppa-use-acf-choice-label" to the CSS Class Name field setting for any field that should use the
 *    ACF choice label.
 */
add_filter( 'gppa_process_template_value', function( $template_value, $field, $template_name, $populate, $object, $object_type, $objects ) {

	if ( strpos( $field->cssClass, 'gppa-use-acf-choice-label' ) === false ) {
		return $template_value;
	}

	$label = get_field( str_replace( 'meta_', '', $field->{'gppa-values-templates'}['value'] ), $object->ID );

	return $label;
}, 10, 7 );

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.