Support

Account

Home Forums General Issues Extract datas from ACF uploaded file

Solving

Extract datas from ACF uploaded file

  • I use ACF file field to import a GPX file, and use a custom php file to :
    1) extract GPS datas (distance, elevation, etc)
    2) auto-update 6 other ACF fields, using this code added in functions.php, when article is saved.

    `require_once(‘gpx_converter/parseGpx.php’);

    add_action( ‘save_post’, ‘update_acf_field_value’);
    function update_acf_field_value(){

    $url = get_field(‘fichier_gpx’, $post->ID)[‘url’];
    $gpxStr = file_get_contents($url);
    $r = parseGpx($gpxStr);
    //print_r (parseGpx($gpxStr));

    $lenght = $r->distance;
    $lenght2 = ceil($lenght);
    $d_plus = $r->d_plus;
    $d_plus2 = ceil($d_plus);
    $d_moins = $r->d_moins;
    $d_moins2 = ceil($d_moins);
    $z_min = $r->z_min;
    $z_min2 = ceil($z_min);
    $z_max = $r->z_max;
    $z_max2 = ceil($z_max);

    update_field(‘distance’,$lenght2);
    update_field(‘denivele_positif’,$d_plus2);
    update_field(‘denivele_negatif’,-$d_moins2);
    update_field(‘altitude_min’,$z_min2);
    update_field(‘altitude_max’,$z_max2);
    }

    Problem : first time article is saved, ACF file doesn’t seem to be loaded -> datas can’tbe extracted -> fields cannot be updated.

    Error message :
    Warning: file_get_contents(): Filename cannot be empty

    Does anyone know how to solve this problem ?

  • When dealing with ACF fields you should almost always use the acf/save_post hook rather than the WP save_post hook. The wordpress hook fires before ACF has saved fields.

    Not only will the values not be saved, but the values you are updating will be overwritten by any values, or empty values, that have been submitted.

  • Thank you for your answer !
    Just changing WP-hook / ACF-hook, and it works fine !
    After many days of brainstorming !
    Thank you again !

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.