Support

Account

Home Forums ACF PRO how to export ACF fields definition in JSON using PHP

Helping

how to export ACF fields definition in JSON using PHP

  • Hi,

    I use ACF embedded in my themes/plugins. For registering the ACF fields at runtime I use the JSON file created using tools/export feature of ACF.

    I would like to automatize the building process, so I would like to have a PHP code that giving the ACF Group IDs generate the related JSON.

    Thanks

  • You can use the function acf_get_field_group($id) to get a field group. $id can be either the field group key or the post ID of the field group.

    You can use the function acf_get_fields($id) to get the fields in the field group. Again $id is either the field group key or the post ID of the field group.

    Putting them together

    
    $field_group = acf_get_field_group($id);
    $field_group['fields'] = acf_get_fields($id);
    

    If you want to just json encode them you’d just do

    
    $json = json_encode($field_group);
    

    If you want the save it to a file and you want it pretty the way that ACF saves field groups, ACF has another function that you can use.

    
    $json = acf_json_encode($field_group);
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘how to export ACF fields definition in JSON using PHP’ is closed to new replies.