Home › Forums › Feature Requests › Warning if a field group has unsync’ed changes
I love the local JSON files feature and use it in all my projects, both ones where I’m the only developer and others where we are several developers.
What we run into ever so often is that we end up losing changes because somebody modifies an ACF field group that has newer changes in the local JSON file that had not been sync’ed yet. For example when working on different branches and switching between them, or when different developers make changes to the field groups and commit the changes to .git.
Luckily, we all our JSON files in git, so we can always go back and find the changes that have been overwritten. But it’s not our favorite way of spending our time 🙂
P.S.: I’ve already implemented a hook for this as part of my projects where I now show a warning on the “Edit field group” screen if the field group has unsync’ed changes. But I think it would be amazing to have something similar in the core product to everyone’s benefit. See the attached screenshot to see how my own little hack looks.
The code for my workaround:
// Show a warning when editing an ACF Field Group that has not been sync'ed yet
function unsynced_acf_field_group_warning_hook() {
global $post;
$group_key = $post->post_name;
$json = acf_get_local_json_files($post->post_type);
if (isset($json[$group_key])) {
// HACK: At this point in the life cycle, the ACF field groups are not loaded
// So we simply read the JSON file and compare the timestamps
$json_file = $json[$group_key];
if (file_exists($json_file)) {
$json_data = json_decode(file_get_contents($json_file), true);
$json_modified = $json_data['modified'] ?? 0;
$db_modified = get_post_modified_time('U', true, $post->ID);
if ($json_modified > $db_modified) {
add_action('admin_notices', function () {
echo '<div class="notice notice-error"><p>The local JSON file has unsynced changes. <a href="' . admin_url('edit.php?post_type=acf-field-group&post_status=sync') . '">Sync now</a></p></div>';
echo '<style> #wpcontent { background: rgba(255 0 0 / 0.05); } </style>';
});
}
}
}
}
add_action('acf/field_group/admin_head', 'unsynced_acf_field_group_warning_hook');
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.