The Json Force Sync module allows developers to always keep Json files synchronized with the Field Groups in the database.
This new logic is particularly useful for developers working with version control since everytime a json file is updated, the changed are automatically saved in the database.
The Force Sync module is disabled by default. It can be enabled and disabled in the Settings UIPRO or with the following code:
// Using acf/init
add_action('acf/init', 'my_acfe_modules');
function my_acfe_modules(){
// Enable Force Sync
acf_update_setting('acfe/modules/force_sync', true);
}
// Or using acfe/init
add_action('acfe/init', 'my_acfe_modules');
function my_acfe_modules(){
// Enable Force Sync
acfe_update_setting('modules/force_sync', true);
}
By default the synchronization will be triggered when a user visits the Admin Dashboard or the ACF Field Groups UI. This behavior can be changed using the acfe/modules/force_sync/rule
hook. Usage example:
/*
* Force Sync Screen Rule
*
* @bool $rule Trigger field groups sync
* @object $screen WP Current Screen Object
*/
filter('acfe/modules/force_sync/rule', $rule, $screen);
add_filter('acfe/modules/force_sync/rule', 'my_acfe_force_sync_screen', 10, 2);
function my_acfe_force_sync_screen($rule, $screen){
// Enable on Dahboard, Posts List & Pages List
if(acf_is_screen(array('dashboard', 'edit-post', 'edit-page'))){
return true;
}
// Disable other rules
return false;
}
It is possible to exclude specific Field Groups from the automatic synchronization using the acfe/modules/force_sync/exclude
hook. Usage example:
/*
* Force Sync Exclude Field Group
*
* @array $exclude List of excluded field groups keys
*/
filter('acfe/modules/force_sync/exclude', $exclude);
add_filter('acfe/modules/force_sync/exclude', 'my_acfe_force_sync_exclude');
function my_acfe_force_sync_exclude($exclude){
// Exclude
$exclude[] = 'group_608abf9d03cb3';
return $exclude;
}
It is possible to also sync deleted Field Groups Json files with the database, and automatically delete corresponding Field Groups.
The Sync Deleted Json Files setting is disabled by default. It can be enabled and disabled in the Settings UIPRO, or with the following code:
// Using acf/init
add_action('acf/init', 'my_acfe_modules');
function my_acfe_modules(){
// Enable Force Sync Deleted Json
acf_update_setting('acfe/modules/force_sync/delete', true);
}
// Or using acfe/init
add_action('acfe/init', 'my_acfe_modules');
function my_acfe_modules(){
// Enable Force Sync Deleted Json
acfe_update_setting('modules/force_sync/delete', true);
}
Developers can manually trigger the Force Sync using the acfe_force_sync()
helper. Please note that this function has to be called after the ACF & ACF Extended initialization. The init
hook is a safe starting point.
If you decide to manually change Json files, note that you need to update the modified
timestamp to a more recent date along with your changes, in order to trigger the ACF Sync & ACFE Force Sync features.
For example, the modified
timestamp 1637788093
tells ACF that the last time the file was updated was the 24/11/2021 at 19:08:13.
If you make a change in the Json file using your text editor, you have to set a date that is newer than the one in the database. Most of the time you can simply add 1 second to that setting. So ACF will compare it with the date from Field Group in the database and understand that there is a newer version. In this example, 1637788093
should become at least 1637788094
.
The process is automatic when the Field Group is updated from the ACF Admin UI, since this modified
setting is updated to the current timestamp.
You can use online tools such as epochconverter.com to convert a timestamp to a readable date.