Skip to content

Instantly share code, notes, and snippets.

@FrancoStino
Last active June 12, 2024 08:11
Show Gist options
  • Save FrancoStino/a2f701b28859096cb968f4b8e4c50d0b to your computer and use it in GitHub Desktop.
Save FrancoStino/a2f701b28859096cb968f4b8e4c50d0b to your computer and use it in GitHub Desktop.
This code snippet adds an action to the admin_init hook. It retrieves field groups, finds JSON field groups which have not yet been imported, and then imports fields from a group based on their local or private values in it. The sync array is also

Sync ACF Fields on Admin Init

Preview:
/**
 * Synchronizes ACF field groups from JSON files.
 *
 * This function is hooked to the `admin_init` action and is responsible for
 * finding and importing any ACF field groups that have been defined in JSON
 * files but have not yet been imported into the database.
 *
 * The function first retrieves all the field groups defined in the application,
 * and then iterates through them to find any that are defined in JSON format
 * and have not yet been imported. For each of these field groups, it retrieves
 * the local fields and then imports the field group using the `acf_import_field_group`
 * function.
 */

add_action('admin_init', function () {
  // Get field groups
  $groups = acf_get_field_groups();
  if (empty($groups))
    return;

  $sync = array();

  // Find JSON field groups which have not yet been imported
  foreach ($groups as $group) {
    $local = acf_maybe_get($group, 'local', false);
    $modified = acf_maybe_get($group, 'modified', 0);
    $private = acf_maybe_get($group, 'private', false);

    // Ignore DB / PHP / private field groups
    if ($local === 'json' && !$private) {
      if (!$group['ID'] || ($modified && $modified > get_post_modified_time('U', true, $group['ID'], true))) {
        $sync[$group['key']] = $group;
      }
    }
  }

  if (empty($sync))
    return;

  foreach ($sync as $key => $group) {
    if (acf_have_local_fields($key)) {
      $sync[$key]['fields'] = acf_get_local_fields($key);
    }
    acf_import_field_group($sync[$key]);
  }
});
Associated Context
Type Code Snippet ( .php )
Associated Tags admin_init jp_sync_acf_fields acf_get_field_groups json field groups db / PHP/private field groups get_post_modified_time sync array acf_have_local_fields ACF _has_local_fields Framework: Advanced Custom Fields (ACF) sync ACF fields import field groups
💡 Smart Description This code snippet adds an action to the admin_init hook. It retrieves field groups, finds JSON field groups which have not yet been imported, and then imports fields from a group based on their local or private values in it. The sync array is also
This PHP function syncs JSON field groups for Advanced Custom Fields on admin initialization.
🔎 Suggested Searches PHP function to sync field groups in admin_init
Related Links https://www.sitepoint.com/sending-emails-php-phpmailer/
https://www.php.net/manual/en/language.variables.superglobals.php
https://www.php.net/manual/en/function.session-set-save-handler.php
Related People Davide Ladisa
Sensitive Information No Sensitive Information Detected
Shareable Link https://davideladisa.pieces.cloud/?p=56b24f821c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment