PHP AutoSync

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.

Json AutoSync

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 new & existing field groups, in order to stick to the native ACF behavior.

Custom Save/Load paths

It is possible to alter the saving & loading to custom path for both PHP and Json Sync. Usage example:

/*
 * PHP Sync: Save Path
 */
add_filter('acfe/settings/php_save', 'my_acfe_php_save_point');
function my_acfe_php_save_point($path){
    
    return get_stylesheet_directory() . '/my-php-folder';
    
}

/*
 * PHP Sync: Load Path
 */
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;
    
}

/*
 * Json Sync: Save Path
 */
add_filter('acfe/settings/json_save', 'my_acfe_json_save_point');
function my_acfe_json_save_point($path){
    
    return get_stylesheet_directory() . '/my-json-folder';
    
}

/*
 * Json Sync: Load Path
 */
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;
    
}

It is also possible to target a specific field group using the following named hooks:

/*
 * PHP Sync: Specific Save
 * 
 * @string  $path         Save folder
 * @string  $field_group  Field group settings
 */

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';
    
}
/*
 * Json Sync: Specific Save
 *
 * @string  $path         Save folder
 * @string  $field_group  Field group settings
 */

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';
    
}

Set Settings in PHP

It is possible to enable the PHP/Json Sync in PHP using the acf/load_field_group hook. Note that this hook is used everywhere, including in ACF admin and tools screen. Usage example:

add_filter('acf/load_field_group', 'my_acf_field_group');
function my_acf_field_group($field_group){
    
    // Target a specific key
    if($field_group['key'] !== 'group_5f20935b9a777')
        return $field_group;
    
    // Enable PHP & Json Sync
    $field_group['acfe_autosync'] = array('php', 'json');
    
    // Return
    return $field_group;
    
}

Retrieve Settings

The PHP & Json AutoSync settings are saved in the Field Group array, under acfe_autosync key. It can be retrieved using acf_get_field_group(). Usage example:

$field_group = acf_get_field_group('group_5f20935b9a777');

/*
 * [instruction_placement] => label
 * [hide_on_screen] =>
 * ...
 * [acfe_autosync] => Array
 * (
 *     [0] => php
 *     [1] => json
 * )
 * ...
 */