Support

Account

Home Forums General Issues Hook for AFC Sync? Reply To: Hook for AFC Sync?

  • I don’t know if this will help you at all. What I mean is, build your own hook that you can trigger some way, but I don’t know if you can do that either.

    For example, I created a function that runs and imports field groups when a theme is activated that looks something like this.

    
    <?php 
      
      // when the theme is switched to this one
      // add an action to admin_init to import groups
      add_action('after_switch_theme', 'theme_name_activation');
      function theme_name_activation() {
        add_action('admin_init', 'theme_name_acf_auto_import_groups');
      } // end function theme_name_acf_auto_import_groups
      
      function theme_name_acf_auto_import_groups() {
        // most of the following is taken directly from ACF
        // get all existing ACF field groups
        $groups = acf_get_field_groups();
        if (empty($groups)) {
          return;
        }
        $sync = array();
        foreach ($groups as $group) {
          $local = acf_maybe_get($group, 'local', false);
          if ($local !== 'json') {
            // field group is not loaded from json
            // skip this one
            continue;
          }
          if (!$group['ID']) {
            // only do import on groups loaded from DB
            $sync[$group['key']] = $group;
          }
        } // end foreach $group
        if (empty($sync)) {
          // no groups to sync
          return;
        }
        foreach ($sync as $key => $v) {
          if (acf_have_local_fields($key)) {
            // sync the fields
            $sync[$key]['fields'] = acf_get_local_fields($key);
          }
          // sync the group
          $field_group = acf_import_field_group($sync[$key]);
        } // end foreach sync
      } // end function theme_name_acf_auto_import_groups
    

    If there is a hook that you can trigger the you could potentially use something similar