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.
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.
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):
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.
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
acf/inithook to call a function that runs
acf_add_local_field_groupon that PHP I exported
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
@joshc4 @hube2 I found this really useful – enables you to convert back and forwards as needed https://dev-notes.eu/2017/01/convert-acf-fields-registered-by-php-to-importable-json-format/
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2022 Advanced Custom Fields.