Support

Account

Home Forums General Issues Hook for AFC Sync?

Solving

Hook for AFC Sync?

  • Hi there.

    I’m integrating a CI/CD pipeline using a shell script (bash) and I’d like to know if there’s some way to trigger the “Sync” for the Field Groups using some kind of PHP/ACF hook or similar – without entering the WP Admin.

    Thanks in advance.

  • ACF does not have any automated way to trigger field group syncing. My only suggestion would be to look at the code that ACF uses and build something.

  • Yep, I tried doing that, checking out the HTTP GET request I found out the way ACF does it but since the Sync is triggered inside the Admin, the pipeline just can’t connect to WP.

    I guess I’d keep doing it by hand until some official solution is provided (which I guess won’t happen soon).

  • 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

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

The topic ‘Hook for AFC Sync?’ is closed to new replies.