Skip to content

Instantly share code, notes, and snippets.

@thisislawatts
Last active June 8, 2017 20:25
Show Gist options
  • Save thisislawatts/2364054d191d062a6f14b711714083eb to your computer and use it in GitHub Desktop.
Save thisislawatts/2364054d191d062a6f14b711714083eb to your computer and use it in GitHub Desktop.
Convert ACF php files to ACFJSON
<?php
function convert_ACF_PHP_2_Json() {
$status = [];
$theme_dir = get_stylesheet_directory();
$json_output_dir = $theme_dir . '/acf-json/';
if (!file_exists($json_output_dir)) {
die('Create a acf-json/ directory in your current theme.');
}
$fieldgroups = glob($theme_dir . '/field-groups/*');
foreach ( $fieldgroups as $fg ) {
$field = $fg . '/data.php';
if ( file_exists($field) ) {
// Include $field into current context
// this will create a $group var
include($field);
if (file_put_contents($json_output_dir . $group['id'] .'.json', json_encode($group) )) {
$status[] = 'Created json file ' . $group['id'] . ' for ' . $group['title'] . '.';
}
}
}
if (count($status)) {
echo implode("<br/>", $status);
} else {
echo 'No ACFJson files created.';
}
die();
}
convert_ACF_PHP_2_Json();
@thisislawatts
Copy link
Author

thisislawatts commented Feb 22, 2017

Paste this function into your theme and it will convert field groups to JSON. Assumes that your field groups are structured as:

field-groups/
  field/
    data.php
  field2/
    data.php
  …

Uses a die() so will break your site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment