Support

Account

Home Forums General Issues How to auto sync json acf groups in multisite network?

Solving

How to auto sync json acf groups in multisite network?

  • Hi Guys,

    I have a multisite setup with 30+ subsites and a solic acf groups, some of which are huge flex content groups and sync slowly.

    I’m looking for automated json sync across the wrole multisite network.
    I’ve tried few plugins but none work.
    I almost managed to make it work (with some fixes in the plugin src) with advanced-custom-fields-auto-json-sync but i think it gets triggered few times in parallel and any huge json files gets synced multiple times and cause duplicates.

    Do you know if there is any built in way/function/hook which i can use to trigger auto sync in multisite network or any plugin working with recent acf pro plugin with huge json files?

    Thanks

  • Hi Guys,

    Does anyone have an idea?

    Thanks

  • I do not sync field groups to all sites in a mutlisite installation. There is not really any need to do this.

    This is what I do.

    I create a theme with an acf-json folder and I activate this theme on only one site. This is my dev site. This is the only place where changes to a field group are made.

    Any other site in the network that uses this theme is set up using a child theme. Part of the parent theme code is checking to see if a child theme is being loaded and if it is then it adds a load point to load the field groups from the parent theme. No syncing to these sites is needed.

  • Actually, even if you’re not using a parent/child theme setup, there isn’t any need to sync field groups to every site. This is a waste of DB space because the only place where the DB version of a field group is used when a JSON version exists is when you are editing that field group. Editing in more than one place just over-complicates dev.

  • Hi John,

    I’ve tested your advice and indeed i don’t need to sync the updates to all sites but just the main subsite. However, i’d still like to have the auto sync option as i’m looking to build automated release process to different environments, which as final step would include automatic filed group syncing. Any ideas about this?

    Thanks

  • Hi John,

    Yes, I tried this implementation and with few changes it works. However for huge field groups (many flex content components) which sync for 5-10 seconds which causes duplications of the group (it appears two or more times). This is due to the async nature of the implementation. The hook gets triggered few times before completion of the fist triggered sync:

    public function maybe_update_field_groups_from_json( $json_dir )

    Any idea how to deal with this?

    Thanks

  • I’ve done this is the past. What you need to do is test to see if the field group exists before importing and it also helps to do the importing only on theme activation.

    I actually gave up on this. I keep a dev version of all my themes on a single multisite installation where I do work. I commit changes to all of these themes to github and I use github updater to allow updating the themes on any sites where they are installed.

    I don’t have the code any more, I found one filter left in one theme

    
    add_action('after_switch_theme', '_ssi_theme_activation');
    function _ssi_theme_activation() {
    	add_action('admin_init', 'acf_auto_import_groups');
    }
    

    So basically, if I remember right, on theme switch I added an admin_init action. In that function I used acf_get_field_groups() to get all of the existing field groups. Then I read through the acf-json folder and looked for field groups that did not already exist to import.

  • @hube2 The plugin does exactly this – it checks if the group doesn’t exist and then imports it. However if the group json file is huge and due to it’s async nature, the function is called few times and this leads to duplicates.

    So you are saying that if it is done only on theme activation it would be fine?

  • I think it should be. Because the hook would only be run once so it should not be possible for the function to run multiple times unless you deactivate and reactivate the theme.

  • Hi John,

    This doesn’t work for me either.
    The flow we are using is as follows:

    1. Devs do local changes and push the json files to a repo.
    2. The changes get automatically deployed to a dev and at some point at prod env.
    3. When the wp admin gets refreshed, the changes are suppose to be auto sync’ed to the given environment.

    The acf groups do get update, however many of them are showing multiple times in the Field Groups page. The acf fields are showing just fine in e new post/page, however i’m not sure if multiplication of groups would cause issues or not, but it doesn’t look nice.

    I saw that there were other topics in the forum regarding this same problem, so there must be some clean solution.

    Thanks, for trying to help.

  • As I said previously, I think, if the field groups are never edited in the production environment, or the staging for that matter, the there is no reason to sync the field groups into the database. This creates unnecessary database content that will never be used. Field groups are never loaded from the DB if a JSON file exists for it unless you are editing the field group through the ACF admin of the site.

  • Hi @hube2 – do i understand correctly that if the groups don’t exist in DB there is not need to trigger sync or try to implement autosync for that matter and things will work correctly?
    If so, can you advice how should i clean up the DB on my existing staging/prod envs so that this works as expected?

    Is reading from json instead of DB more optimal?

    Thanks

  • Make sure you have a good copy of all the JSON files. Go into ACF, trash all of the field groups, then delete permanently. Once that is done upload the acf-json folder to the site.

    Yes, reading from JSON files gives better performance. Reading the files is faster than making the DB queries needed to load the field groups.

  • @hube2

    I do not sync field groups to all sites in a mutlisite installation. There is not really any need to do this.

    This is what I do.

    I create a theme with an acf-json folder and I activate this theme on only one site. This is my dev site. This is the only place where changes to a field group are made.

    Any other site in the network that uses this theme is set up using a child theme. Part of the parent theme code is checking to see if a child theme is being loaded and if it is then it adds a load point to load the field groups from the parent theme. No syncing to these sites is needed.

    I’m trying this solution, but even with all plugins disabled I keep getting endless loading (white screen) when I activate the child theme and visit the front-end.

    I’ve tried manual child theme and a plugin “child theme creator” but neither work. Any ideas? I’m nog 100% knowledgable on multisites.

  • @daveoudshoorn without code there’s no way for me to know. There should not be anything that causes infinite loading.

  • I was using get_stylesheet_directory_uri in the parent theme so it threw it for a loop 😉

    That said, I currently have this setup:

    Network admin
    – English “main site” with the parent theme enabled
    – German, child-theme from EN
    – Russian, child-theme from EN

    However, when I add/edit ACF Custom Fields they don’t sync/show up in the DE/RU version. Am I missing something?

  • @hube2 – I guessed it would work automatically, but I re-read your post and saw this:

    Part of the parent theme code is checking to see if a child theme is being loaded and if it is then it adds a load point to load the field groups from the parent theme.

    Could you elaborate a bit on this, how would I set this up?

  • Rather than build code in the child theme that adds a load point I have this code in the parent theme to reduce code in the child theme.

    In the funcitons.php file of the parent theme I have

    
    add_filter('acf/settings/load_json', 'parent_theme_acf_json_load_point);
    function parent_theme_acf_json_load_point($paths) {
      if (!is_child_theme()) {
        return $paths;
      }
      array_unshift($paths, get_template_directory().'/acf-json');
      return $paths;
    } 
    

    The reason I use array_unshift() is to ensure that the parent json files will be first and override the child theme because I do not allow changes to these field groups in the child theme. If more fields are needed for a specific child theme then these field must be created by adding additional field groups to the child theme.

    This could be done in the child theme, but like I said, it’s added code in every child theme. To me this is like the way most people build themes where when using a child theme the developer must do extra work to enqueue the parent theme’s stylesheet when they can simply check in the parent theme to see if a child theme is being used and automatically add both the parent theme’s stylesheet and the child theme’s stylesheet in the correct order with the correct dependencies to begin with. Little QOL improvements to keep me sane and reduce my workload.

  • That’s brilliant, seems to work well – thanks!

  • Hi.
    Thank you for this solution, it works really well!

    The only thing I need is for the syncing to happen automatically. When I change Field Groups on the main site (with parent theme), for now I can see a notification that syncing is available, and I need to do it manually on each subsite (with child theme).

    The problem will be when the network will have hundreds of subsites.

    Can you help?
    Thanks

  • Thanks for this solution. It’s work really well !!

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

The topic ‘How to auto sync json acf groups in multisite network?’ is closed to new replies.