Created
February 3, 2025 17:24
-
-
Save saifsultanc/2d1172b846f2f3f38222958db5890375 to your computer and use it in GitHub Desktop.
gw-populate-acf-to-gf-address-for-field-value-object.php
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
<?php | |
add_filter( 'gppa_get_batch_field_html', 'capture_acf_address', 10, 6 ); | |
function capture_acf_address( $html, $field, $form, $fields, $entry_id, $hydrated_field ) { | |
// Replace 5 with your form id and 3 with your ACF address field id | |
if ( $form['id'] != 5 && $field['id'] != 3 ) { | |
return $html; | |
} | |
$field_id = $field->id; | |
$value_1 = isset( $_POST['input_' . $field_id . '.1'] ) ? $_POST['input_' . $field_id . '.1'] : ''; | |
$value_2 = isset( $_POST['input_' . $field_id . '.2'] ) ? $_POST['input_' . $field_id . '.2'] : ''; | |
$value_3 = isset( $_POST['input_' . $field_id . '.3'] ) ? $_POST['input_' . $field_id . '.3'] : ''; | |
$value_4 = isset( $_POST['input_' . $field_id . '.4'] ) ? $_POST['input_' . $field_id . '.4'] : ''; | |
$value_5 = isset( $_POST['input_' . $field_id . '.5'] ) ? $_POST['input_' . $field_id . '.5'] : ''; | |
$value_6 = isset( $_POST['input_' . $field_id . '.6'] ) ? $_POST['input_' . $field_id . '.6'] : ''; | |
// Split the input string based on commas | |
$parts_1 = explode( ',', $value_1 ); | |
$parts_2 = explode( ',', $value_2 ); | |
$parts_3 = explode( ',', $value_3 ); | |
$parts_4 = explode( ',', $value_4 ); | |
$parts_5 = explode( ',', $value_5 ); | |
$parts_6 = explode( ',', $value_6 ); | |
// Extract the parts according to your conditions | |
$address_line_1 = isset( $parts_1[9] ) ? trim( $parts_1[9] ) : ''; | |
$address_line_2 = isset( $parts_2[10] ) ? trim( $parts_2[10] ) : ''; | |
$city = isset( $parts_3[12] ) ? trim( $parts_3[12] ) : ''; | |
$state = isset( $parts_4[13] ) ? trim( $parts_4[13] ) : ''; | |
$zip = isset( $parts_5[15] ) ? trim( $parts_5[15] ) : ''; | |
$country = isset( $parts_6[16] ) ? trim( $parts_6[16] ) : ''; | |
$dom = new DOMDocument(); | |
libxml_use_internal_errors(true); | |
$dom->loadHTML($html); | |
$inputs = $dom->getElementsByTagName('input'); | |
$select = $dom->getElementById('input_' . $form['id'] . '_' . $field_id . '_6'); | |
$field_values = [ | |
'input_' . $field_id . '.1' => $address_line_1, | |
'input_' . $field_id . '.2' => $address_line_2, | |
'input_' . $field_id . '.3' => $city, | |
'input_' . $field_id . '.4' => $state, | |
'input_' . $field_id . '.5' => $zip, | |
'input_' . $field_id . '.6' => $country | |
]; | |
foreach ( $inputs as $input ) { | |
if ( $input instanceof DOMElement ) { | |
$name = $input->getAttribute('name'); | |
if ( isset( $field_values[$name] ) ) { | |
$input->setAttribute( 'value', htmlspecialchars( $field_values[$name] ) ); | |
} | |
} | |
} | |
if ( $select ) { | |
foreach ( $select->getElementsByTagName('option') as $option ) { | |
if ( $option instanceof DOMElement && $option->getAttribute('value') === $country) { | |
$option->setAttribute('selected', 'selected'); | |
} | |
} | |
} | |
$html = $dom->saveHTML(); | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment