Last active
June 8, 2017 20:25
-
-
Save thisislawatts/2364054d191d062a6f14b711714083eb to your computer and use it in GitHub Desktop.
Convert ACF php files to ACFJSON
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 | |
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Paste this function into your theme and it will convert field groups to JSON. Assumes that your field groups are structured as:
Uses a
die()
so will break your site.