Map ACF Google Maps Field to Address Field

Maps an ACF Google Maps field’s data to individual Address field inputs when using the Update User Registration feed.

Instructions

  1. Map your ACF Google Maps meta key to the Address field using “Full Address” option
  2. Update the form ID and field ID in the configuration below

Code

Filename: gfur-map-acf-google-maps-to-address-field.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
/**
 * Gravity Forms // User Registration // Map ACF Google Maps Field to Address Field
 * https://gravitywiz.com/
 *
 * Maps an ACF Google Maps field's data to individual Address field inputs
 * when using the Update User Registration feed.
 *
 * Instructions:
 * 1. Map your ACF Google Maps meta key to the Address field using "Full Address" option
 * 2. Update the form ID and field ID in the configuration below
 */
add_filter( 'gform_user_registration_user_data_pre_populate', function( $mapped_fields, $form, $feed ) {
	// Configuration
	$target_form_id   = 123; // Your form ID
	$address_field_id = 4;   // Your Address field ID

	if ( $form['id'] != $target_form_id ) {
		return $mapped_fields;
	}

	// When mapped to "Full Address", ACF data arrives as an array at the parent field ID
	$address_data = rgar( $mapped_fields, $address_field_id );

	if ( ! is_array( $address_data ) || empty( $address_data['city'] ) ) {
		return $mapped_fields;
	}

	// Build street address from components
	$street = trim( rgar( $address_data, 'street_number' ) . ' ' . rgar( $address_data, 'street_name_short' ) );
	if ( empty( $street ) ) {
		$street = rgar( $address_data, 'name' );
	}

	// Map to Address field inputs
	$mapped_fields[ $address_field_id . '.1' ] = $street;
	$mapped_fields[ $address_field_id . '.2' ] = '';
	$mapped_fields[ $address_field_id . '.3' ] = rgar( $address_data, 'city' );
	$mapped_fields[ $address_field_id . '.4' ] = rgar( $address_data, 'state_short' );
	$mapped_fields[ $address_field_id . '.5' ] = rgar( $address_data, 'post_code' );
	$mapped_fields[ $address_field_id . '.6' ] = rgar( $address_data, 'country' );

	return $mapped_fields;
}, 10, 3 );

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.