Support

Account

Home Forums Gutenberg Local JSON in ACF Blocks

Solved

Local JSON in ACF Blocks

  • Hi everyone,

    Please excuse me if this has previously been covered elsewhere in the forum.

    What I’m trying to achieve (if possible), is multiple ACF blocks within a plugin that use their own stand-alone local.json file. e.g Image Block has acf.json that houses only Image Block group fields.

    I don’t need to use syncing, nor do I want the block group fields to sit within ACF WP admin. Completely stand-alone.

    Where I’ve got to – I’ve setup a multi block plugin using @wordpress/create-block and have a couple of test blocks in there. The field groups have been setup and display correctly within block editor. I’ve exported each field group separately through the ACF tools.

    As I haven’t been able to figure out yet how to copy json files to build folder with @wordpress/create-block, so I have added json exports to a folder “acf-json” to the root folder of my plugin for now.

    I’ve used these filters outlined in ACF docs:

    //Change ACF Local JSON save location to /acf folder inside this plugin
    add_filter('acf/settings/save_json', function() {
    	return dirname(__FILE__) . '/acf-json';
    });
    
    //Include the /acf folder in the places to look for ACF Local JSON files
    add_filter('acf/settings/load_json', function() {
    	$paths[] = dirname(__FILE__) . '/acf-json';
    	return $paths;
    });

    The above works correctly, but issue is the field groups are required to be in the ACF field groups admin and all field groups are then synced (including non block groups).

    Now to my question is (if possible) – How can I set each block to load it’s own unique json file?

    Appreciate any insight!

    Cheers

  • You cannot use exported JSON files for this. To add local JSON to a plugin.

    1) add the acf-json folder to your theme
    2) create field groups
    3) download the json files for you field groups from your theme folder
    4) upload the json files to the acf-json folder in your plugin
    5) Add the path to your plugin json files with acf/settings/load_json

    Note, you cannot use acf/settings/save_json the way you are using it. This will cause all field groups on the site to be saved in your plugin folder. In order to do this you must check the field group field group to make sure that it is one of the field groups used by your plugin before adding the save location hook. This can be done using the acf/update_field_group filter to check the field group and if it is one of yours then add the acf/settings/save_json filter.

    If you do not want the field groups to by sync-able then you add then you can set them as private, that is discussed here https://www.advancedcustomfields.com/resources/synchronized-json/. This must be done manually because when saving a field group in the admin, if you are using acf/settings/save_json hook then the private setting will be removed when the groups is saved in the admin. It is possible to use the acf/prepare_field_group_for_export hook to create a filter that can set the private value of the field group dynamically based on something else. I have done this in the past by defining a custom constant on my dev sites so that I can check this and set the private value based on this constant being defined.

  • Hey @hube2,

    Thanks for the info, I’ll look further into your solution!

    I was also recommend through the ACF support to register the fields using PHP instead as shown at https://github.com/programer-alvin/2022-Child-theme/tree/master/acf/blocks/acf_v6/all-fields.

    Thanks for your help on this!

    Cheers,

    Dan

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.