Use this snippet to convert legacy field groups from PHP to ACF-JSON files

I was working on a legacy project recently that had a multiple Advanced Custom Fields – field groups registered with PHP. It was difficult to edit these hard coded field groups so I decided it was best to switch these to JSON. I ran┬áthe snippet below once, it converts each of the field groups from PHP into JSON and save these JSON files to the acf-json┬ádirectory.

<?php 

// 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_local_fields( $key );

	}

	// save the acf-json file to the acf-json dir by default 
	acf_write_json_field_group( $field_group );

}
?>					

Keep up to date with all things ACF!

Subscribe to our newsletter or follow @awesomeacf on Twitter!