I am completely lost with JSON sync. I’m either not understanding its purpose or implementing it incorrectly.
I have been trying to apply code from this post
https://support.advancedcustomfields.com/forums/topic/acf-json-fields-not-loading-from-parent-theme/
I have a multisite with two sites and many field groups.
90% of the field groups between the two sites are identical/shared. Of the 90% some sub fields are different such as the number of rows, or if a field is required. And then there are the 10% of items that should be different and not shared.
I decided to try out JSON sync because I’m tired of using file comparison software to check for consistency, but also to make sure some settings aren’t applied where they shouldn’t be – and I want to put the files under version control as this makes sense to track changes.
First issue:
I implemented ACF JSON sync by adding the acf-json folder in my theme. However, it overwrites one set of ACF fields with the other set of data whereas before they were independent. Which meant one site update removed fields in the other site – presumably because originally I was using the JSON export method and when you import to the other site it uses the same field group IDs.
Second issue:
Realising the above I decided to learn how to set up child templates purely as an attempt to separate the writing and loading of ACF JSON between the sites.
So I set up two child sites and followed the snippet of code in the link above and added the snippet of code in (only) the parent theme functions.php file.
I’m not seeing any files appear in the acf-json directory in the root of my child themes, but I do see updates to the parent theme acf-json folder and the same scenario happens.
What I have noticed is that if new field groups are created in one site they don’t appear in the other for syncing, but anything that already existed appears to be related.
Does this method of allowing JSON sync really only work when you use it from the ground up and not retrospectively?
Am I better off trying to keep what’s similar between two sites as a set of shared fields for sync, and re-creating anything specific to one site with a prefixed field group?
add_filter('acf/settings/save_json', function() {
return get_stylesheet_directory() . '/foo';
});
add_filter('acf/settings/load_json', function($paths) {
$paths = array(get_template_directory() . '/foo');
if(is_child_theme())
{
$paths[] = get_stylesheet_directory() . '/foo';
}
return $paths;
});
In your case you will need json folders for all of the sites.
In the parent theme you will have the field groups where everything is identical on all sites. You can create a function/action in the parent theme to add this “load point”. You can only work on and edit these field groups in the admin of the site that is using the parent theme.
Then you will have 2 child themes, one for each site. These will have their own json folders. You only edit the field groups in the admins of these sites where the field groups are not identical.
Your other choice is to keep all of the field groups, the ones that are the same and the ones that are different in the parent theme and instead of creating a load point for ACF you would build your own function to add the field groups to a site. In this function you would look at the site currently being loaded and only include the field groups you want to apply to that site.
Hope some of that makes sense.
Thanks for your help John.
I’m marking your response as resolved because it’s helped me come to a decision, and made me realise how the site should have been built. However, due to time restraints I can’t explore your proposed solutions, and I don’t think the time it would take me to find the best alternative (potentially a day) would be re-gained in any time saved in future deployments.
I don’t get how currently what I do works and copied changes from site to site are isolated between database tables but when you apply json sync it overwrites due to the field groups being the same.
After much thought it’s evident that the structure of the field groups in the site I’m working on is beyond repair unless I wanted to do a couple of days data entry in re-creating posts after re-grouping fields and then re-write all the field name look ups in the php, not to mention all the testing.
The site was built a couple of years ago (not by me), quite a lot of posts have been created using field groups that have been shared and subsequent fields have been added in the same field group. In the main post there are 22 fields in one field group for instance, and in the parallel site there are 25. When I activated json sync I was horrified to find that it overwrote the fields and subtly different settings such as number of rows in repeater fields and which fields were set as required. This is because from the very beginning the ACF export option had been used to copy across fields and then the appropriate modifications were made, all in complete isolation. It appears you have more control over manually copying and then modifying/removing field groups between sites from the out of the box database way than using json sync. I guess this is because ACF is not built in mind for multisite and any solutions for multisite are work arounds.
To summarise, the option of splitting unique field groups up by defining them within child themes could work, but in my circumstance of just two sites (but potentially more in time) and 90% of field groups being shared, but with subtle differences I’ll have more control doing things the manual way and double checking than relying on json sync.
The lesson learned would be – when working on future sites (especially wordpress multisite) – to make field groups as smaller, more portable, specific use components rather than cramming everything into one mega field group. Same principle as when writing functions when programming!
I don’t get how currently what I do works and copied changes from site to site are isolated between database tables but when you apply json sync it overwrites due to the field groups being the same.
The files saved in the json folder reflect whatever you saved last. If two sites are using the same folder then they both pull from those last saved changes. This is based on the field group key. Two sites with the same field groups but that have different fields in them cannot share a folder.
It appears you have more control over manually copying and then modifying/removing field groups between sites from the out of the box database way than using json sync. I guess this is because ACF is not built in mind for multisite and any solutions for multisite are work arounds.
Yes. Basically, when using ACF on multisite you have completely different db entries. If the sites share a theme and you attempt to use local json then basically, you can only have to sites with identical field groups and fields. As you say, having two sites with the same theme requires work-a-rounds.
My guess is that when the site was built was assumed that all sites would use the same fields and at some point along the way this assumption was proven false and the developer did what he needed to to make it work. Changing requirements usually result it this happening. I’m having a similar situation myself.
I actually didn’t think about this site until I read your reply.
I have a site that was originally built as a single site installation. It was then converted to mutlisite because the client wanted to add additional sites using the same theme. Unfortunately, this means that all the field groups need to be copied to every new site. I was already using local json so that wasn’t really an issue. Since the fields were not in the DB for the new site it just used the json files from the theme. Then, what has you are seeing I had to start making “adjustments” to the fields for each site. This has resulted in needing to add many filters to adjust fields for the different sites. In this case I have created an options page for each site. This options page contains a list of fields that need to be changed. Each field on the options page includes setting for “should the field be shown”, and the field “label” to use for each site and in some cases what the values of the field can be. The I use the acf/load_field to modify the field. I also use the acf/prepare_field filter to remove fields from specific sites where that is needed. On top of this all of the templates on the front end need to have additional checks added to see if a particular field should be shown or not. This results in giving me the ability to work on all of the field groups in the main site and be able to adjust those fields as needed for every other site. Every time I have to make an adjustment I have to also make adjustments to the options page and the filters. It’s all very complicated and the complication has grown over an extended period of time. I can’t imagine what the next developer will do when they inherit this site.
The topic ‘JSON sync in multisite’ is closed to new replies.
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.