Skip to content

Instantly share code, notes, and snippets.

@1naveengiri
Last active January 13, 2019 10:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1naveengiri/1e3f49b60ce7fdcfa3ee7a297cbd2def to your computer and use it in GitHub Desktop.
Save 1naveengiri/1e3f49b60ce7fdcfa3ee7a297cbd2def to your computer and use it in GitHub Desktop.
Convert ACF Fields Registered by PHP to Importable JSON Format
<?php
/**
* create a file acf-import.json inside your theme main folder
* use this code in themes functions.php or in any custom plugin.
* It is one time code so it will add all fields json entry inside acf-import.json file
*/
add_action('init', 'create_fieldjson_from_fieldphp');
function create_fieldjson_from_fieldphp() {
$groups = acf_get_local_field_groups();
$json = [];
foreach ($groups as $group) {
// Fetch the fields for the given group key
$fields = acf_get_local_fields($group['key']);
// Remove unecessary key value pair with key "ID"
unset($group['ID']);
// Add the fields as an array to the group
$group['fields'] = $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>";
// Write output to file for easy import into ACF.
// The file must be writable by the server process. In this case, the file is located in
// the current theme directory.
$file = get_template_directory() . '/acf-import.json';
file_put_contents($file, $json );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment