Remove Hidden Non-breaking Spaces

Remove hidden non-breaking spaces from your field values. This snippet currently only applies to multi-selectable choice fields (e.g. Checkboxes, Multi-selects) and resolves an issue where choices in these fields were not correctly selected when strings contained nbsp’s between values.

Code

Filename: gppa-remove-hidden-nbsp.php

<?php
/**
 * Gravity Perks // Populate Anything // Remove Hidden Non-breaking Spaces
 * https://gravitywiz.com/documentation/gravity-forms-populate-anything/
 *
 * Remove hidden non-breaking spaces from your field values. This snippet currently only applies to multi-selectable
 * choice fields (e.g. Checkboxes, Multi-selects) and resolves an issue where choices in these fields were not correctly
 * selected when strings contained nbsp's between values.
 */
add_filter( 'gppa_process_template_value', function( $value, $field ) {
	if ( ! is_array( $value ) && in_array( $field->get_input_type(), gp_populate_anything()::get_multi_selectable_choice_field_types(), true ) ) {
		$value = htmlentities( $value, null, 'utf-8' );
		$value = str_replace( '&nbsp;', '', $value );
		$value = html_entity_decode( $value );
	}
	return $value;
}, 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.