Skip to content

Instantly share code, notes, and snippets.

@JodiWarren
Created September 23, 2015 09:36
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JodiWarren/1f45208fc8841fc01cb5 to your computer and use it in GitHub Desktop.
Save JodiWarren/1f45208fc8841fc01cb5 to your computer and use it in GitHub Desktop.
<?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.<';
}
@ollietreend
Copy link

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