I'm trying to populate an ACF field on save (updating or creating the post) while running a geolocate function using mapbox. I'm getting a cannot modify header information error when hitting the update button.
Warning: Cannot modify header information - headers already sent by (output started at C:\****\public\wp-content\themes\***\mapbox\GeocodeResponse.php:22) in C:\****\public\wp-admin\post.php on line 223
My two files are brought in like this:
require_once get_template_directory() . '/mapbox/Mapbox.php';
require_once get_template_directory() . '/mapbox/custom-geocode.php';
Code in my custom-geocode.php:
function geolocate_test( $post_id ) {
$mapbox = new Mapbox("mykey");
$field_group = get_field('address');
$address = $field_group['line_1'] . ' ' . $field_group['line_2'] . ' ' . $field_group['city'] . ', ' . $field_group['state'] . ' ' . $field_group['zip'] . ' ' . $field_group['country'];
$res = $mapbox->geocode($address);
$long = $res[0]['center'][0];
$lat = $res[0]['center'][1];
$value = [
'latitude' => $lat,
'longitude' => $long
];
update_field('address', $value, $post_id );
}
add_action( 'save_post', 'geolocate_test' );
I'm using this same exact code elsewhere when updating taxonomies using, create_taxonomy & edit_taxonomy, so I'm not sure why I'm getting this error when saving/updating a post.
Any help is much appreciated!