Skip to content

Instantly share code, notes, and snippets.

@jessepearson
Created March 24, 2015 12:58
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jessepearson/a537b2f78556cd705947 to your computer and use it in GitHub Desktop.
Automatically update Advanced Custom Fields field groups via JSON
<?php
/**
* Function that will automatically update ACF field groups via JSON file update.
*
* @link http://www.advancedcustomfields.com/resources/synchronized-json/
*/
function jp_sync_acf_fields() {
// vars
$groups = acf_get_field_groups();
$sync = array();
// bail early if no field groups
if( empty( $groups ) )
return;
// find JSON field groups which have not yet been imported
foreach( $groups as $group ) {
// vars
$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 ) {
// do nothing
} elseif( ! $group[ 'ID' ] ) {
$sync[ $group[ 'key' ] ] = $group;
} elseif( $modified && $modified > get_post_modified_time( 'U', true, $group[ 'ID' ], true ) ) {
$sync[ $group[ 'key' ] ] = $group;
}
}
// bail if no sync needed
if( empty( $sync ) )
return;
if( ! empty( $sync ) ) { //if( ! empty( $keys ) ) {
// vars
$new_ids = array();
foreach( $sync as $key => $v ) { //foreach( $keys as $key ) {
// append fields
if( acf_have_local_fields( $key ) ) {
$sync[ $key ][ 'fields' ] = acf_get_local_fields( $key );
}
// import
$field_group = acf_import_field_group( $sync[ $key ] );
}
}
}
add_action( 'admin_init', 'jp_sync_acf_fields' );
@leup
Copy link

leup commented Jun 14, 2016

Nice ! It allowed me to update field group on plugin activation.

Very handy. Thanks for sharing !

@nkkollaw
Copy link

Hi!

I've implemented syncing when groups are deleted as well as updated: https://gist.github.com/nkkollaw/8f0b0047fb6fc4a000bc975719fec682

Usage:

add_filter('acf/settings/save_json', function() {
	return '/path/to/json/';
});
add_filter('acf/settings/load_json', function($paths) {
	$paths[0] = '/path/to/json/';

	return $paths;
});

add_action('admin_init', function() {
	$json_dirs = array('/path/to/json/');

	jp_sync_acf_fields($json_dirs);
});

@Jaballadares
Copy link

Thank you so much for posting this! It will save our team tons of time.

You are appreciated 💯

@justinkmotion
Copy link

I was running into an issue with it not syncing subfields of repeaters here:
$sync[ $key ][ 'fields' ] = acf_get_local_fields( $key );

Changing it to this works:
$sync[ $key ][ 'fields' ] = acf_get_fields( $key );

@amjad
Copy link

amjad commented May 22, 2019

Here is a better modified version of this:
https://gist.github.com/amjad/0d8bd7e9108001defc5fed546787a17b

@Jaballadares
Copy link

justinkmotion thanks for posting that fix. I ran into the same issue with the subfields of repeaters and flexible content.

@saminton
Copy link

Neither this nor @amjad's or @justinkmotion's solutions are working correctly for me when using flexible content and clones. It keeps randomly deleting acf fields.

@jessepearson
Copy link
Author

@saminton It's most likely due to code changes. I wrote this four years ago it looks like for a need I had at the time.

@amjad
Copy link

amjad commented Aug 30, 2019

@saminton yes it did not work for certain field types for me as well so I stopped using this and went back to the PHP file method.

@juanjosezg
Copy link

@saminton yes it did not work for certain field types for me as well so I stopped using this and went back to the PHP file method.

How did you implemented?

@saminton
Copy link

saminton commented Feb 3, 2020

@juanjosezg I ended up using https://www.advancedcustomfields.com/resources/local-json/ . It's not automated (you have to press a sync button) but it's the best I could find that worked reliably.

@Mulli
Copy link

Mulli commented Feb 24, 2020

@saminton Can you please provide a better link. The current is broken. Thanks

@saminton
Copy link

@Mulli, sorry I messed up the hyperlink. All fixed now.

@mnngithub
Copy link

acf_get_fields
@justinkmotion That worked. However all of my acf field groups got duplicated. How do i solve this?

@souricardomaia
Copy link

@justinkmotion thanks to post the fix. Help me a lot.

@souricardomaia
Copy link

Hey everyone,

  1. Did anyone have problems with automatic duplication of groups after using this script?

  2. I noticed that some groups started to appear as posts. Did this happen to any of you?

ACF error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment