How to get Advanced Custom Fields to load JSON files from more than one directory
Last updated • October 8th, 2019
If you use Advanced Custom Fields a lot you are probably using ACF’s very awesome Local JSON feature.
By default, ACF saves and loads all JSON files from the acf-json directory inside your theme but it is possible to take control of ACF’s behaviour to define your own custom load points as outlined in the Advanced Custom Fields documentation.
By adding your own directories, you are able to define many points within your app that can’t be easily overridden by people working with the custom fields in the WordPress admin. Being able to load from many directories also makes it possible for you to bundle your field groups into component-based directories which you could use to build and maintain a library of portable UI components…ooh-la-la!
How to register your own ACF JSON load points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
As you can see from the above snippet, it’s super easy to define your own ACF JSON load directories.
Auto registering all subdirectories as ACF JSON load points
Let’s say you have a components directory which contains multiple directories, each representing a component, and each of these components contain their own ACF JSON files. An easy way to handle loading JSON files from each would be to simply read the directory with PHP and register each subdirectory:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters