Support

Account

Home Forums Backend Issues (wp-admin) How to disable auto JSON sync for some field groups?

Solving

How to disable auto JSON sync for some field groups?

  • Hi. I recently set up JSON sync for my field groups and I was pretty happy with the feature. I need only some field groups to be synced, so I was thinking that I would manually check and sync only those I wanted, that way I’d be able to keep control on them. However, I noticed that all field groups seemed to sync automatically, which isn’t a behaviour I want. If I save a field group on a site, the change will directly appear on my other sites as well.

    Is there a way I could disable that auto sync feature? Or maybe, if that’s not possible, disabled sync only for some field groups? I have to disable the whole syncing for now since I don’t want it to break all my sites.

    Thank you for your help 🙂 Best regards

  • Hi @tahoe

    According to the docs:

    If you do not wish for your field group to qualify for ‘available for sync’, simply add an extra value to the JSON array called ‘private’: ‘private’ : true. This may become useful for plugin / theme developers who wish to keep their bundled field groups hidden from the user.

  • I think the OP is talking about using this on multisite with a single theme that has an acf-json folder. But I’m just guessing. I really would not be possible to have a master field group that can be editable per site and different on every sub site without having a different child theme folder for every site.

  • Hi! Thank you for the quick response 🙂

    @jarvis I didn’t know about this! It’s interesting. I tried it and it indeed makes the field group disappear from those available for sync. Problem is that if I save the field group again (which I do all the time), the private value will disappear and they’ll start automatically syncing again. I think it would work if there was a way to check a “private” checkbox directly from the field group edit page, but I’m not sure how to do it.

    @hube2 What I have is a multisite with a main theme, and every subsite has a child theme. Each subsite has an options page with three subpages (each of them containing one field group). I want to sync only one of those subpages, the other two need to be modified frequently to fit their site’s needs.

    In the main theme’s functions.php, I put two things: a filter to change the saving location of the JSON, and another one to change the loading location, like this:

    
    add_filter('acf/settings/save_json', 'my_acf_json_save_point');
    function my_acf_json_save_point($path) {
        // update path
        $path = get_template_directory() . '/custom-fields';
        
        // return
        return $path;
    }
    
    // changes loading location for custom fields
    add_filter('acf/settings/load_json', 'my_acf_json_load_point');
    function my_acf_json_load_point( $paths ) {
        // remove original path (optional)
        unset($paths[0]);
        
        // append path
        $paths[] = get_template_directory() . '/custom-fields';
        
        // return
        return $paths;
    }

    Hope that’s clear! Again, thank you for your help

  • The first thing you need to do is to add an acf-json folder to the child theme.

    The second is to not unset the child them load point

    
    
    // changes loading location for custom fields
    add_filter('acf/settings/load_json', 'my_acf_json_load_point');
    function my_acf_json_load_point( $paths ) {
        // remove original path (optional)
        // do not do this
        //unset($paths[0]);
        
        // append path
        $paths[] = get_template_directory() . '/custom-fields';
        
        // return
        return $paths;
    }
    

    What will happen. JSON will be loaded from the child theme first, only field groups that do not exist in the child theme will be loaded by the parent theme.

    Then you need to alter the save point code to only alter the save point for the fields you want to always load from the parent theme and not be overridden by the child theme. This takes 2 filters

    The first filter adds the save point filter only to selected field groups when they are saved

    
    add_action('acf/update_field_group', 'my_acf_json_update_field_group', 1, 1);
    function my_acf_json_update_field_group($group) {
      // list of field groups you want to save to parent theme
      $groups = array(
        'group_12345678',
        'group_23456789',
        // etc...
      );
      if (in_array($group['key'], $groups)) {
        add_filter('acf/settings/save_json',  'my_acf_json_save_point', 9999);
      }
      return $group;
    }
    

    remove your other add_filter line

    
    add_filter('acf/settings/save_json', 'my_acf_json_save_point');
    

    and this will only affect the field groups specified in the first filter

    
    
    function my_acf_json_save_point($path) {
      // remove this filter so that it will not affect other field groups
      remove_filter('acf/settings/save_json',  'my_acf_json_save_point', 9999);
        // update path
        $path = get_template_directory() . '/custom-fields';
        
        // return
        return $path;
    }
    
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.