Last active
March 10, 2020 09:04
-
-
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
This file contains 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 | |
// 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' ); |
Credits:
https://www.awesomeacf.com/snippets/convert-an-acf-field-group-from-php-to-json/
But the code didn't worked with subfields: repeater/flexible...
Fix by: @justinkmotion
found in: https://gist.github.com/jessepearson/a537b2f78556cd705947
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script allows you to get your ACF PHP Fields back in the Admin.
acf-json
directory in your theme (create the folder manually if nothing happens)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.