Skip to content

Instantly share code, notes, and snippets.

@alex-authlab
Last active January 13, 2023 15:44
Show Gist options
  • Save alex-authlab/ad5985f800c02e927dd34f13c3fad33c to your computer and use it in GitHub Desktop.
Save alex-authlab/ad5985f800c02e927dd34f13c3fad33c to your computer and use it in GitHub Desktop.
Fluent Form populate address fields from API
add_filter('fluentform_rendering_field_data_address', function ($data,$form)
{
// your form id
if($form->id != 1) {
return $data;
}
// the base adress field name
$addressField = 'address_1';
if( $data['attributes']['name'] != $addressField ){
return $data;
}
$provider = 'https://geolocation-db.com/json/';
$request = wp_remote_get($provider.$_SERVER['REMOTE_ADDR'], [] );
if(is_wp_error($request)) {
return $data; // request failed so we are skipping
}
$response = wp_remote_retrieve_body($request);
$response = json_decode($response, true);
if( $response ) {
$data['fields']['city']['attributes']['value'] = $response['city'];
$data['fields']['country']['attributes']['value'] = $response['country_code'];
$data['fields']['zip']['attributes']['value'] = $response['postal'];
$data['fields']['state']['attributes']['value'] = $response['state'];
}
return $data;
}, 10, 3);
@JOY
Copy link

JOY commented May 8, 2021

Could anyone tell me how to use this code?

@alex-authlab
Copy link
Author

Use this inside your theme function.php or in any PHP code snippet plugin for the WP Fluent Form plugin addresses autocomplete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment