Skip to content

Instantly share code, notes, and snippets.

@mishterk
Last active June 21, 2024 19:28
Show Gist options
  • Save mishterk/d3d3010b21771a94340d03738d5b0566 to your computer and use it in GitHub Desktop.
Save mishterk/d3d3010b21771a94340d03738d5b0566 to your computer and use it in GitHub Desktop.
Convert an ACF Field Group from PHP to ACF-JSON
<?php
// Get all the local field groups and loop through them for processing.
$field_groups = acf_get_local_field_groups();
foreach ( $field_groups as $group ) {
// If the field group has fields, load them into the 'fields' key.
if ( acf_have_local_fields( $group['key'] ) ) {
$group['fields'] = acf_get_local_fields( $group['key'] );
}
// Write the group's JSON file.
acf_write_json_field_group( $group );
}
@daniloparrajr
Copy link

@mishterk This doesn't work on repeater sub fields though

@mishterk
Copy link
Author

@daniloparrajr, I suspect you may need to loop through fields within the field group and if they have sub fields, assign those to the field's sub_fields key.

  1. Have you tried inspecting the $group['fields'] value after it is assigned to see if acf_get_local_fields() gets sub fields as well?
  2. Have you dug into ACF at all to see how ACF handles local field groups for clues?

If you find a solution, suggest an adjustment so we can update the snippet.

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