fbpx
Awesome

Forcing ACF field group JSON file generation for all field groups

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 );

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!';
} );

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.

This is a Hookturn plugin ad block

This is a Hookturn plugin ad block

Built by Hookturn

This site was created by Hookturn, the team behind ACF Theme Code Pro. If you are interested in contributing or have an extension to add, email us.

Sponsored by Pressidium

Awesome ACF is sponsored by the very awesome team at Pressidium. If you need reliable, secure and scalable Managed WordPress Hosting, check them out.

Stay up to date with all things ACF

TODO: Registration form here