Created
May 22, 2020 14:13
-
-
Save celsowhite/deafe580ea149177258f3b722d171ca0 to your computer and use it in GitHub Desktop.
Convert ACF Fields Registered via PHP to JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*----------------------------- | |
Notes | |
- After running the script then remove the "ID" and "_valid" keys from all fields in the json output. These are extraneous and may cause import errors. | |
Resource - https://dev-notes.eu/2017/01/convert-acf-fields-registered-by-php-to-importable-json-format/ | |
-----------------------------*/ | |
$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 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