Mapping booleans to custom user profile fields using User Registration add-on

I have a form to update user profiles. The profile has some additional fields (created in Pods), including some booleans; I’ve created equivalent form fields.

I am trying to maps the form fields to the profile fields, and these booleans don’t seem to make it through—in fact, I need to enter the field names by hand as “custom metas” because they do not appear in the popup list.

Hi there,

Could you please share the more details including fields, meta keys, Gravity Forms export copy so I can try to help you out?

If you want to know more about me, I’m here over 3 years and helping peoples, also contributed to the Pods plugin in recent time. You can check from below.

1 Like

Faisal–

Thanks. I am appending a screenshot of the relevant Pods fields. The Gravity Forms export is a little too big to include here, so I’ve uploaded it here.

Although Gravity Forms lets you place multiple checkboxes under a single title, for clarity I’ve created a separate title for each checkbox in the form.

Let me know if I can provide any more information.

Hi @adamrice

Sorry for the delay. I’ve already resolved the issue for the single checkbox item. Could you please share your all Pods by exporting so I can import on my end and give you a complete solution?

Thank you

Hi again,

I’ve resolved the issue. Now you can select the Pods fields under Gravity Forms > Settings > User Registration > User Meta, as shown in the screenshot below.

After a user is created or updated, the user meta will auto-sync and can be found under the user profile.

Please use the following code as an MU-Plugin for now. I plan to release a small plugin later for easier use in the future.

<?php
/**
 * Plugin Name: Gravity Forms + Pods Integration
 * Description: Automatically adds Pods User fields to the User Registration Add-On meta keys list, and properly casts mapped Checkbox values into strict booleans (1/0) for Pods boolean fields.
 * Author: Faisal Ahammad
 */

// 1. Add Pods User fields to the GF User Registration Meta Key dropdown
add_filter( 'gform_user_registration_user_meta_options', function( $keys ) {
	if ( ! function_exists( 'pods_api' ) ) {
		return $keys;
	}

	$api = pods_api();
	// Load the Extended User pod (if it exists)
	$pod = $api->load_pod( [ 'name' => 'user' ], false );
	if ( ! empty( $pod ) && ! empty( $pod['fields'] ) ) {
		if ( ! is_array( $keys ) ) {
			$keys = [];
		}
		
		foreach ( $pod['fields'] as $field ) {
			$keys[] = [
				'label'    => '[Pods] ' . $field['label'],
				'name'     => $field['name'],
				'value'    => $field['name'],
				'required' => false
			];
		}
	}

	return $keys;
}, 10, 1 );

// 2. Intercept boolean values to ensure they save as 1/0 for Pods
add_filter( 'gform_user_registration_meta_value', function( $value, $meta_key, $meta, $form, $entry ) {
	if ( ! function_exists( 'pods_api' ) ) {
		return $value;
	}

	$api = pods_api();
	$pod = $api->load_pod( [ 'name' => 'user' ], false );
	if ( empty( $pod ) || empty( $pod['fields'] ) || empty( $pod['fields'][ $meta_key ] ) ) {
		return $value; // Not a Pods field or not registered.
	}

	$field = $pod['fields'][ $meta_key ];

	// Only apply processing to boolean fields
	if ( 'boolean' === $field['type'] ) {
		if ( is_array( $value ) ) {
			$value = empty( $value ) ? 0 : 1;
		} else {
			// In Gravity Forms, an unchecked string is usually empty "".
			// A explicitly false string could be "false" or "no". 
			// Valid labels are considered truthy.
			$value = ( empty( $value ) || '0' === $value || false === filter_var( $value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) ) ? 0 : 1;
		}
	}

	return $value;
}, 10, 5 );

You can also get the source code from here:

MU-Plugin guideline:

Give it a try, and let me know how that goes! :grinning_face_with_smiling_eyes:

Best regards,
Faisal

1 Like

Thanks very much. I haven’t had a chance to try this out yet, but I appreciate your time and expertise.

You’re welcome.

Please keep me posted about the outcome. :+1: