0

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!

  • Your page is trying to redirect but failing, because the headers are already sent - which likely means either an error is being output (maybe check your error logs) or possibly you just have white space before an opening PHP tag. – vancoder Sep 16 at 21:29
  • What is on line 22 in wp-content\themes\***\mapbox\GeocodeResponse.php – Cyclonecode Sep 17 at 1:48
  • @Cyclonecode $this->resultCount = count($this->body['features']); – user3330820 Sep 17 at 12:51
  • That entire file can be seen here: github.com/mapbox/geocoding-example/blob/master/php/… – user3330820 Sep 17 at 13:15
  • Well this works fine for me so I guess you must have some error output, try enabling debug in your wp-config.php e.g define('WP_DEBUG', true); define('WP_ERROR_LOG', true); and see if you get any error messages in wp-conten/debug.log. – Cyclonecode Sep 17 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.