Serialize data from checkboxes on user profile [RESOLVED]

Hello there,

I’m having trouble with a form designed to create users. I use the Register Users Add-on and in this form I have a multiple checkbox field corresponding to an ACF checbox field.
The data saved in the database is a comma separated string: C1, C2, C3…
I need it to be serialized but my function does not work…
Can’t figure out what the data from the form on user activation looks like?

Here is my code:

add_action( 'gform_user_registered', 'waw_add_user_cours', 10, 3 );
function waw_add_user_cours( $user_id, $feed, $entry ) {

	// Form Field ID
	$cours = rgar( $entry, '17' );

	if ( $cours ) {
		
		// Convert the comma separated string to an array.
		$cours_array = explode( ', ', $cours );
		
		// Serialize the array for ACF.
		$cours_serialized = maybe_serialize( $cours_array );

		// Update user meta.
		update_user_meta( $user_id, 'les_cours', $cours_serialized);
		
		    if ( true === WP_DEBUG ) {
		      error_log( print_r( $cours, true ) );		
	        }
	}
}

In debug.log, $cours is empty…

Reference Update a post custom field with serialized GF checkboxes or Multi Select for a similar use case. Note specifically use of the method $field->get_value_export(). Give something like this a try…

add_action( 'gform_user_registered', 'waw_add_user_cours', 10, 3 );
function waw_add_user_cours( $user_id, $feed, $entry ) {

    // Form Field
    $field = GFAPI::get_field( rgar( $feed, 'form_id' ), 17 );

    // Form Field ID
	$cours = $field->get_value_export( $entry );

	if ( $cours ) {

		// Convert the comma separated string to an array.
		$cours_array = explode( ', ', $cours );

		// Update user meta – WP will serialize
		update_user_meta( $user_id, 'les_cours', $cours_array);
		
	}

}

Thanks a lot Joshua,

I updated my code without :

// Serialize the array for ACF.
$cours_serialized = maybe_serialize( $cours_array );

It’s now working great :slight_smile:

1 Like