Unserialize as Choices

Convert serialized data into choices when populated into a choice-based field.

Usage

  1. Install this code as a plugin or as a snippet.
  2. Add the gppa-unserialize-as-choices designator to your field’s CSS Class Name setting.

Code

Filename: gppa-unserialize-as-choices.php

<?php
/**
 * Gravity Perks // Populate Anything // Unserialize as Choices
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * If you have a serialized value mapped to a choice-based field, Populate Anything will populate it as a single value.
 * This snippet adds the `gppa-unserialize-as-choices` designator to allow you to specify when serialized values
 * should be converted into choices.
 *
 * Usage:
 *
 * 1. Install this code as a plugin or as a snippet.
 * 2. Add the `gppa-unserialize-as-choices` designator to your field's CSS Class Name setting.
 *
 * Plugin Name:  GP Populate Anything – Unserialize as Choices
 * Plugin URI:   https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 * Description:  Convert serialized data into choices when populated into a choice-based field.
 * Author:       Gravity Wiz
 * Version:      0.1
 * Author URI:   https://gravitywiz.com
 */
add_filter( 'gppa_input_choices', function ( $choices, $field ) {

	if ( strpos( $field->cssClass, 'gppa-unserialize-as-choices' ) === false ) {
		return $choices;
	}

	$new_choices = array();

	foreach ( $choices as $choice ) {
		$values = maybe_unserialize( $choice['value'] );
		if ( ! $values ) {
			continue;
		}
		foreach ( $values as $value ) {
			$new_choices[] = array(
				'text'       => $value,
				'value'      => $value,
				'isSelected' => false,
			);
		}
	}

	return $new_choices;
}, 10, 2 );

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.