Skip to content

Instantly share code, notes, and snippets.

@maximebj
Last active March 10, 2020 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximebj/cbf44763cd8d0c84820795885726fa34 to your computer and use it in GitHub Desktop.
Save maximebj/cbf44763cd8d0c84820795885726fa34 to your computer and use it in GitHub Desktop.
WordPress ACF PHP Fields back to Admin with JSON • Works with newer ACF versions
<?php
// This script allows you to get your ACF PHP Fields back in the Admin
$json = "";
// get all the local field groups
$field_groups = acf_get_local_field_groups();
// loop over each of the gield gruops
foreach( $field_groups as $field_group ) {
// get the field group key
$key = $field_group['key'];
// if this field group has fields
if( acf_have_local_fields( $key ) ) {
// append the fields
$field_group['fields'] = acf_get_fields( $key );
$json .= json_encode( $field_group, JSON_PRETTY_PRINT );
}
// save the acf-json file to the acf-json dir
acf_write_json_field_group( $field_group );
}
// (optionnal) Echo json to see output
echo "<pre>$json</pre>";
// (Optionnal) Change ACF JSON export directory
// Also allow to auto-export groups when modified
// You should keep this hook at anytime
function my_json_save_groups( $path ) {
$path = get_stylesheet_directory() . '/acf-json';
return $path;
}
add_filter( 'acf/settings/save_json', 'my_json_save_groups' );
@maximebj
Copy link
Author

maximebj commented Jan 29, 2020

This script allows you to get your ACF PHP Fields back in the Admin.

  1. Execute this script once to export all fields to JSON fields
  2. You'll find them in a acf-json directory in your theme (create the folder manually if nothing happens)
  3. Go to ACF > Tools > Import and import theses files
  4. Et voilà !

The acf/settings/save_json allows you to change the export directory (default acf-json) but also to auto export groups when a change is made. Very useful for versionning fields structure and settings.

@maximebj
Copy link
Author

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