Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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.<';
}
@blizzrdof77
Copy link

blizzrdof77 commented Jan 20, 2017

Any way to do this reverse (JSON to PHP)?

@JodiWarren
Copy link
Author

JodiWarren commented Feb 23, 2017

@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.

@ollietreend
Copy link

ollietreend commented May 10, 2017

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