Skip to content

Instantly share code, notes, and snippets.

@nathanieltubb
Last active November 19, 2019 13:50
Show Gist options
  • Save nathanieltubb/236f73eeb8f0f7bcc71cc33da06e0b8b to your computer and use it in GitHub Desktop.
Save nathanieltubb/236f73eeb8f0f7bcc71cc33da06e0b8b to your computer and use it in GitHub Desktop.
Return ACF Field Groups In JSON If You Only Have PHP. Revised code from this post David Egan - https://dev-notes.eu/2017/01/convert-acf-fields-registered-by-php-to-importable-json-format/
<?php
include_once '../wp-load.php';
$groups = acf_get_local_field_groups();
$json = [];
foreach ($groups as $group) {
// Fetch the fields for the given group key
$fields = acf_get_fields($group['key']);
// Remove unnecessary key value pair with key "ID"
unset($group['ID']);
$new_fields = [];
foreach($fields as $field) {
// Remove unnecessary key value pair with key "ID"
unset($field['ID']);
$new_fields[] = $field;
}
// Add the fields as an array to the group
$group['fields'] = $new_fields;
// Add this group to the main array
$json[] = $group;
}
//
$json = json_encode($json, JSON_PRETTY_PRINT);
// Optional - echo the JSON data to the page
echo "<pre>";
echo $json;
echo "</pre>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment