Support

Account

Home Forums General Issues Can I sync ACF fields AND values using local JSON?

Solving

Can I sync ACF fields AND values using local JSON?

  • Hey ACF friends!

    I run a multisite install and would like to sync Options page fields and values between a handful of my sites.

    I’ve setup a child theme and made use of the wonderful local JSON feature to enable this group of fields and Options on all the required sites.

    I’m now just needing to sync these values across all the sites in question. Is it possible to do this using the local JSON or does that only share the fields and not the required values?

    What am I missing here?

    Many thanks in advance!

    Chris

  • JSON only shares the fields and not any data that is save to those fields.

    When dealing with this you need to build the default values into your theme.

    example:

    
    $option = get_field('your-field-name', 'options');
    if (!$option) {
      $option = 'default option value';
    }
    
  • Thanks John!

    At the moment I’ve got one of the sites acting as a “master” where these values will be maintained and like these to replicate out across the other sites.

    How would I use your snippet above to achieve this using my child theme?

    Thanks again,
    Chris

  • What that does is just use a default. What you want to do is use a value from a different site if not set on the current site.

    What I would do it create an acf/load_value filter for each field, you may be able to use a single filter for multiple fields. You can even use this to update the value that are missing.

    
    // example for text field
    $text_fields = array(
      // list of field keys
      'field_1234',
      'field_4321'
    );
    foreach ($text_fields as $key) {
      add_filter('acf/load_field/key='.$key, 'load_text_field_from_master_site', 20, 3);
    }
    function load_text_field_from_master_site($value, $post_id, $field) {
      if ($post_id != 'options') { // or your appropriate ID
        // only for an options page
        return $value;
      }
      if (!empty($value)) {
        // field has a value
        return $value;
      }
      
      // get value from master site
      switch_to_blog(1); // or the ID of your master site
      $value = get_field($field['name'], $post_id);
      restore_current_blog();
      
      // update value on current site
      update_field($field['key', $value, $post_id);
    
      // return the value
      return $value;
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Can I sync ACF fields AND values using local JSON?’ is closed to new replies.