Created
February 11, 2025 07:01
-
-
Save saifsultanc/16deb10bef80bbaadfb2689302befb81 to your computer and use it in GitHub Desktop.
76963.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_new', 10, 6 ); | |
function capture_acf_address_new( $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'] != 6 && $field['id'] != 3 ) { | |
return $html; | |
} | |
// Replace 1 with the field id of the field you have used for Field Value Object | |
$post_id = rgpost( 'input_1' ); | |
global $wpdb; | |
$meta_value = $wpdb->get_var( | |
$wpdb->prepare( | |
"SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s AND post_id = %d", | |
'acf_address', | |
$post_id | |
) | |
); | |
if ( ! empty( $meta_value ) ) { | |
$address_array = maybe_unserialize( $meta_value ); | |
} | |
$field_id = $field->id; | |
$address_line_1 = rgar( $address_array, 'street_number' );//rgar( $address_array, 'name' ); | |
$address_line_2 = rgar( $address_array, 'street_name' ); | |
$city = rgar( $address_array, 'city' ); | |
$state = rgar( $address_array, 'state' ); | |
$zip = rgar( $address_array, 'post_code' ); | |
$country = rgar( $address_array, 'country' ); | |
$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