Last active
January 13, 2023 15:44
-
-
Save alex-authlab/ad5985f800c02e927dd34f13c3fad33c to your computer and use it in GitHub Desktop.
Fluent Form populate address fields from API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
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
Could anyone tell me how to use this code?