Home Forums ACF PRO Opinion: Register fields via PHP or Local JSON?


Opinion: Register fields via PHP or Local JSON?

  • I’ve been looking at these two methods to speed up ACF a little bit as well as limit clients breaking ACF in the future.

    Is there any different between registering with PHP via functions.php or using a local JSON?

    Or are they essentially the same thing?

    JSON one seems handy as you can create the fields in the admin and export as JSON (this is my understanding) whilst writing out the php seems time consuming and easier to break with a rogue full stop or something?

    Unless there is a boilerplate of all the ACF fields in php ready to just drop into a functions.php file?

  • I personally like using the JSON files because they are easier to change in the future… import the field group, make a change and then export it again.

    I build all of the field groups in ACF, even when I’m going to put them in PHP. Much easier than trying to code them. I also save a copy of the json file should I want to make changes in the future. Although, if I’m putting them in PHP it usually for a specific reason, for example, if something needs to be dynamic, lets say that I also create an options page that lets the user choose what post types and/or templates the field group would be on, or if I want to allow another admin to decide what other features of the page should be available, like page options or feature images.

    The main difference for me between json or php is the ability to translate. If I’m creating a plugin and I want to add my text domain this is fairly easy in PHP, at least for me. I might be missing something, or I just don’t understand the process, but translating the strings in the JSON file would be a much more difficult task, and one that I personally don’t know how to do if it is possible.

  • Thanks John, interesting.

    So i using the php why would probably be best if you wanted some further interaction with WordPress, the JSON way if its just a plain old field.

    Speed wise, both similar and quicker (front end and back??) than a default ACF done in the admin?

  • ACF field groups in the DB create additional queries that need to be performed, slowest as far as performance goes.

    ACF fields in JSON files, for example in an acf-json folder, are loaded first. Reading these files from the folder is significantly faster than loading from the DB. This is why Elliot has added this feature. ACF still needs to do a query to get any groups not loaded this way, or to check to see if each group has already been loaded from JSON. The improved performance comes from not needed to do a query to get all the fields in each group.

    You can create field groups in PHP in one of two ways.

    1) You can read your own JSON files or set another load point for ACF. This is not really different than having them in an acf-json folder in the theme.

    2) You can code them right in PHP, like I said, I usually do this when I’m not going to use the field group “as is” and I might want to alter it is some other way. I don’t know if there is much improvement here. You’re reading one less file, so I’d assume there is, but my guess is that any improvement is insignificant.

  • Thank you John, so loading via PHP and JSON is essentially the same is that what you mean?

    Interesting idea to always have a JSON copy of the php export just incase, i assume storing this in the theme but not importing wouldn’t slow anything down?

  • Yes, json and php for performance are essentially the same.

    I generally create some type of assets folder where I keep things, like json exports. I’m also something of a digital pack rat, so I never throw anything away or delete anything that may come in useful. After needing to rebuild a few field groups in ACF manually by reading the php it didn’t take me long to realize how important a json file of a field groups with a hundred fields was to keep around.

  • Can anyone shed any light on priorities here?

    If you have an acf-json folder AND your ACF fields defined as functions within your theme code – which one takes priority? In fact, if you have these two AND the fields already defined within your WordPress dashboard, what then?



  • During normal order of events is (I think):

    1. JSON
    2. Database

    Where PHP/Local fields fall in this depends on what hook and priority you use for running your functions.

    ACF starts up on “init” with a default priority (10).

    Also there is the ACF hook “acf/include_fields”, and this is actually when the JSON files are loaded.

    To be honest, I do not believe that field groups are actually loaded from the database until they are needed and that your local PHP field groups will always override those in the database. Whether or not they override the JSON versions in any folders depends on if you load your fields before ACF looks at the JSON files or after.

  • Thanks John, that’s useful, and appreciated

  • Sorry if this should be obvious to me, but here’s what I’ve done on a local project, and I’m a little confused

    1. Use ACF Pro Field Groups config (GUI) to create a Flexible Content field group
    2. Export PHP for said group
    3. Use the acf/init hook to call a function that runs acf_add_local_field_group on that PHP I exported
    4. Delete and trash the previously exported field group from ACF Field Groups config, intending to clear that from the DB
    5. Use a template that pulls in fields from that group (still seems to work)

    But now I thought somehow the ACF UI would be regenerated, for further customization, if needed. It isn’t being regenerated.

    I haven’t tried the same with JSON exports (which we’ve previously using in a completely manual way to set up new dev instances).

    Is there a trick to using PHP to regenerate the ACF Field Group config (GUI) for a field group? It’s not something that can be imported, like JSON, and it looks like there’s reference in the above discussion to exporting both PHP and JSON, maybe for the purposes of rebuilding the graphical configuration, but I’m not sure.

    I’d rather stick to one approach (just PHP or JSON). Any advice is welcome


  • fields added using PHP will not appear in the ACF UI/Admin and cannot be modified except by modifying the PHP.

  • Thanks, John. OK, then I’m not doing anything wrong. I will quickly try out the local JSON approach, but understand that we might still need to save exported JSON if we want to fully re-create the ACF Field Groups UI. Or, correct me if I’m wrong about that.

  • You will only be able to update a JSON file if it is in the save path, usually the acf-json folder in the theme. But yes, If you want to be able to edit them using ACF then you’ll need to export them before you delete them.

  • Thanks again. When I have time, I will try working with the local JSON approach.



  • Thank you, @dmje, that looks like a great option.

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

The topic ‘Opinion: Register fields via PHP or Local JSON?’ is closed to new replies.