Awesome ACF

Forcing ACF field group JSON file generation for all field groups

By Phil Kurth · · Updated 11 Nov 2021

If you’ve ever worked on a site with many ACF fields and it hasn’t had ACF JSON enabled, you might find it has to run a lot more database queries than you would expect. This is basically because ACF needs to look up each field in the database in order to then look up the field value which involves additional SELECT queries.

You can cut down on these extra database queries by enabling ACF JSON. When ACF boots, it looks for an acf-json directory within the current active theme. If it finds this directory, it will load up all .json files within and attempt to load the contents of each as field groups and their fields into a map. With the map in place, ACF can avoid unnecessary database calls as it has a reference for all field keys and names. ACF uses the map to understand which field value it needs to find when you make a call such as get_field('my_field').

Enabling ACF JSON is as simple as creating creating an /acf-json directory inside your active theme. ACF checks to see if the directory exists when you save a field group and, if it does, it saves a .json file containing the field group and all its fields.

Saving multiple field groups at once can be a slow process

If you have a site with a lot of field groups, opening each and saving them can be a bit of a pain. Fortunately, we can utilise some convenient functions within ACF and basically generate field group JSON files for all field groups in a few simple lines of code:

<?php 

$field_groups = acf_get_field_groups();

// Apply our callback to all field groups
array_map( function ( $field_group ) {

	// Load up the fields on the field group.
	$field_group['fields'] = acf_get_fields( $field_group );

	// Write the local JSON file for the field group.
  	acf_write_json_field_group( $field_group );

}, $field_groups );

Source: generate-acf-json-files-for-all-field-groups.php

This line of code might be a bit difficult to run on its own so you’ll want to place it in some script or process you can execute. A nice, easy way to do this can be to use an admin post endpoint:

<?php

add_action( 'admin_post_acf_sync', function () {

	$field_groups = acf_get_field_groups();

	// Apply our callback to all field groups
	array_map( function ( $field_group ) {

		// Load up the fields on the field group.
		$field_group['fields'] = acf_get_fields( $field_group );

		// Write the local JSON file for the field group.
		acf_write_json_field_group( $field_group );

	}, $field_groups );

	echo 'done!';
} );

Source: generate-acf-json-files-for-all-field-groups-on-request.php

The above script will basically be runnable by logged in users and can be run by simply visiting yoursite.com/wp-admin/admin-post.php?action=acf_sync in your browser.

From Hookturn, who keep this site

ACF Remote Sync

Keep ACF field group configurations in sync across development, staging, and production environments.

More guides

2023

2017

Stay updated

Get ACF tips and new extensions in your inbox. No spam. Unsubscribe anytime.