ACF Extended streamline the built-in ACF Json feature and add the ACF PHP Sync counterpart. Out of the box, both features become managed, and you can control which Field Group should be synced as JSON and/or PHP.
You can enable the PHP/JSON Sync in your field group declaration from the acfe.autosync key. Usage example:
acf_add_local_field_group(array(
'key' => 'group_my_field_group',
'title' => 'My Field Group',
'active' => true,
'acfe' => array(
'autosync' => array(
'json',
'php',
),
),
));You can retrieve settings using acf_get_field_group() and the acfe_get() helper. Usage example:
$field_group = acf_get_field_group('group_my_field_group');
/**
* $field_group = array(
* 'key' => 'group_my_field_group',
* 'title' => 'My Field Group',
* 'active' => true,
* 'acfe' => array(
* 'autosync' => array(
* 'json',
* 'php',
* ),
* ),
* )
*/
$autosync = acfe_get($field_group, 'acfe.autosync');
/**
* $autosync = array(
* 'json',
* 'php',
* ),
*/ACF Extended allows the automatic creation & inclusion of local PHP declaration for Field Groups. Inspired by the native ACF Json sync feature, this functionnality allow developers to take advantage of the speed of local PHP Field Groups, while maintaining field groups in the back-end.
To enable this feature, you’ll have to create a folder called /acfe-php/ in the active theme folder, then enable the “PHP Sync” setting in the targeted field group. Once activated, the field group will display “php” in the “Load” column, and the PHP Sync will be checked.
#Custom PHP AutoSync Save Path
/**
* acfe/settings/php_save
*
* @string $path Save path
* @array $field_group Field group settings
*/
filter('acfe/settings/php_save', $path);
filter('acfe/settings/php_save/all', $path, $field_group);
filter('acfe/settings/php_save/ID=122', $path, $field_group);
filter('acfe/settings/php_save/key=group_5f50bb1964cd4', $path, $field_group);add_filter('acfe/settings/php_save/key=group_5f50bb1964cd4', 'my_acfe_php_save_point', 10, 2);
function my_acfe_php_save_point($path, $field_group){
return get_stylesheet_directory() . '/my-php-folder';
}#Custom PHP AutoSync Load Path
/**
* acfe/settings/php_load
*
* @array $paths Load paths
*/
filter('acfe/settings/php_load', $paths);add_filter('acfe/settings/php_load', 'my_acfe_php_load_point');
function my_acfe_php_load_point($paths){
// append path
$paths[] = get_stylesheet_directory() . '/my-php-folder';
// return
return $paths;
}
#Should delete the PHP file
Control whenever the PHP file should be deleted when the field group is removed or the PHP Sync setting has been disabled.
/**
* acfe/settings/should_delete_php
*
* @bool $delete Should delete PHP file
* @array $field_group Field group settings
*/
filter('acfe/settings/should_delete_php', $delete, $field_group);
filter('acfe/settings/should_delete_php/ID=122', $delete, $field_group);
filter('acfe/settings/should_delete_php/key=group_5f50bb1964cd4', $delete, $field_group);add_filter('acfe/settings/should_delete_php/key=group_5f50bb1964cd4', 'my_acfe_should_delete_php', 10, 2);
function my_acfe_should_delete_php($delete, $field_group){
return false;
}By default, ACF save Field Groups declaration in Json format when the folder /acf-json/ has been created in the active theme. ACF Extended gives you the control over which Field Group should be automatically synced in Json. To enable the Json Sync, check the “Json Sync” setting in the targeted field group.
Note: By default, if a Json folder has been declared within the theme or the code, ACF Extended will automatically check the “Json Sync” setting on all newly created field groups, in order to stick to the native ACF behavior.
#Custom Json AutoSync Save Path
/**
* acfe/settings/json_save
*
* @string $path Save path
* @array $field_group Field group settings
*/
filter('acfe/settings/json_save', $path);
filter('acfe/settings/json_save/all', $path, $field_group);
filter('acfe/settings/json_save/ID=122', $path, $field_group);
filter('acfe/settings/json_save/key=group_5f50bb1964cd4', $path, $field_group);add_filter('acfe/settings/json_save/key=group_5f50bb1964cd4', 'my_acfe_json_save_point', 10, 2);
function my_acfe_json_save_point($path, $field_group){
return get_stylesheet_directory() . '/my-json-folder';
}#Custom Json AutoSync Load Path
/**
* acfe/settings/json_load
*
* @array $paths Load paths
*/
filter('acfe/settings/json_load', $paths);add_filter('acfe/settings/json_load', 'my_acfe_json_load_point');
function my_acfe_json_load_point($paths){
// append path
$paths[] = get_stylesheet_directory() . '/my-json-folder';
// return
return $paths;
}#Save Json files into the loading folder
Using the hooks above it is possible to automatically save the json files to their respective loading folders. This script is useful when developers have multiple directories & sub directories which load json files. Usage example:
add_filter('acfe/settings/json_save/all', 'my_acfe_save_json_where_its_loaded', 10, 2);
function my_acfe_save_json_where_its_loaded($path, $field_group){
// get all local json files loaded
$files = acf_get_local_json_files();
// check if the field group is found in the local json files
$load_path = acf_maybe_get($files, $field_group['key']);
// local json file found
if($load_path){
// return the directory
return dirname($load_path);
}
// return
return $path;
}#Should delete the Json file
Control whenever the Json file should be deleted when the field group is removed or the Json Sync setting has been disabled.
/**
* acfe/settings/should_delete_json
*
* @bool $delete Should delete Json file
* @array $field_group Field group settings
*/
filter('acfe/settings/should_delete_json', $delete, $field_group);
filter('acfe/settings/should_delete_json/ID=122', $delete, $field_group);
filter('acfe/settings/should_delete_json/key=group_5f50bb1964cd4', $delete, $field_group);add_filter('acfe/settings/should_delete_json/key=group_5f50bb1964cd4', 'my_acfe_should_delete_json', 10, 2);
function my_acfe_should_delete_json($delete, $field_group){
return false;
}