Skip to content

Instantly share code, notes, and snippets.

@benedict-w
Last active March 3, 2020 15:29
Show Gist options
  • Save benedict-w/7801843ac5442d75bb5c1dcfab3683dc to your computer and use it in GitHub Desktop.
Save benedict-w/7801843ac5442d75bb5c1dcfab3683dc to your computer and use it in GitHub Desktop.
Auto generated acf-json from Advanced Custom Fields JSON export
/**
* Auto generated acf-json from Advanced Custom Fields JSON export
* How to use:
*
* 1. Export ACF fields to JSON.
* 2. Rename file "acf-export.json".
* 3. Place in theme root.
* 4. Add this script to functions.php, run once. Check scripts are added to acf-json folder (needs write permissions).
* 5. Remove after successful use!
*/
add_action('init', function() {
if($json = json_decode(file_get_contents(get_stylesheet_directory() . '/acf-export.json'))) {
$path = get_stylesheet_directory() . '/acf-json';
foreach ($json as &$item) {
$fp = fopen(sprintf('%s/%s.json', $path, $item->key), 'w');
fwrite($fp, json_encode($item));
fclose($fp);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment