Created
September 23, 2015 09:36
-
-
Save JodiWarren/1f45208fc8841fc01cb5 to your computer and use it in GitHub Desktop.
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 | |
// put array in here | |
$array = array ( | |
'key' => 'group_54624ad06f391', | |
'title' => 'Company List', | |
'fields' => array ( | |
array ( | |
'key' => 'field_54624aff43237', | |
'label' => 'Companies', | |
'name' => 'companies', | |
'prefix' => '', | |
'type' => 'repeater', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'min' => '', | |
'max' => '', | |
'layout' => 'table', | |
'button_label' => 'Add Row', | |
'sub_fields' => array ( | |
array ( | |
'key' => 'field_54624b0d43238', | |
'label' => 'Company Name', | |
'name' => 'company', | |
'prefix' => '', | |
'type' => 'text', | |
'instructions' => '', | |
'required' => 1, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'placeholder' => '', | |
'prepend' => '', | |
'append' => '', | |
'maxlength' => '', | |
'readonly' => 0, | |
'disabled' => 0, | |
), | |
array ( | |
'key' => 'field_54624b1443239', | |
'label' => 'Sector', | |
'name' => 'sector', | |
'prefix' => '', | |
'type' => 'text', | |
'instructions' => '', | |
'required' => 1, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'placeholder' => '', | |
'prepend' => '', | |
'append' => '', | |
'maxlength' => '', | |
'readonly' => 0, | |
'disabled' => 0, | |
), | |
array ( | |
'key' => 'field_54624b1a4323a', | |
'label' => 'Bio', | |
'name' => 'bio', | |
'prefix' => '', | |
'type' => 'wysiwyg', | |
'instructions' => '', | |
'required' => 1, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'default_value' => '', | |
'tabs' => 'all', | |
'toolbar' => 'basic', | |
'media_upload' => 0, | |
), | |
array ( | |
'key' => 'field_54624b484323c', | |
'label' => 'Logo', | |
'name' => 'logo', | |
'prefix' => '', | |
'type' => 'image', | |
'instructions' => '', | |
'required' => 0, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'return_format' => 'array', | |
'preview_size' => 'thumbnail', | |
'library' => 'all', | |
), | |
), | |
), | |
array ( | |
'key' => 'field_54624b554323d', | |
'label' => 'Filter bar', | |
'name' => 'filter_bar', | |
'prefix' => '', | |
'type' => 'true_false', | |
'instructions' => '', | |
'required' => 1, | |
'conditional_logic' => 0, | |
'wrapper' => array ( | |
'width' => '', | |
'class' => '', | |
'id' => '', | |
), | |
'message' => 'Show Filter Bar', | |
'default_value' => 1, | |
), | |
), | |
'location' => array ( | |
array ( | |
array ( | |
'param' => 'page_template', | |
'operator' => '==', | |
'value' => 'template-companylist.php', | |
), | |
), | |
), | |
'menu_order' => 0, | |
'position' => 'normal', | |
'style' => 'default', | |
'label_placement' => 'top', | |
'instruction_placement' => 'label', | |
'hide_on_screen' => '', | |
); | |
// put absolute path to theme in here | |
$themePath = '/Users/jodi/Sites/examplewebsite/wp-content/themes/mytheme/'; | |
$acfJsonPath = $themePath . 'acf-json/'; | |
$output = json_encode($array); | |
echo '<p>Writing file to ' . $acfJsonPath . $array['key'] . '.json</p>'; | |
$file = file_put_contents( $acfJsonPath . $array['key'] . '.json', $output ); | |
if ($file) { | |
echo "<p>". $file . " bytes file created.</p>"; | |
} else { | |
'<p>Error, file not created.<'; | |
} |
Any way to do this reverse (JSON to PHP)?
@blizzrdof77 I'm not sure, but I'd try running a var_export(json_decode($myJsonFieldDefinition))
- might well work. If not, it might at least be a good start.
I've put together a WordPress plugin which makes this even easier. See my gist acf-php-to-json.php
.
Thanks for the inspiration 😃
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use this to convert from the old
register_field_group
method of defining ACF fields to the new (and much more useful IMO) JSON file method. Paste the contents of a field group into the$array
variable, then set the$themePath
to the absolute path to your theme.You have to do this separately for each field group, so it can be slightly laborious if you have many of them. I tried (for 5 minutes) to parse the entire file, but I couldn’t figure out the regex for extracting each array, so I did it the quick and dirty way.