Skip to content

Instantly share code, notes, and snippets.

@gummiforweb
Last active February 21, 2019 09:49
Show Gist options
  • Save gummiforweb/0399b6ae65793bd4e16fa5f5be16419e to your computer and use it in GitHub Desktop.
Save gummiforweb/0399b6ae65793bd4e16fa5f5be16419e to your computer and use it in GitHub Desktop.
ACF Component Field - Save component in sub folders
<?php
// NOTE: You'd have to create /acf-json/components/ foler in the theme manually first,
// just like how you'd manutally create the acf-json folder
/**
* In /includes/json.php:17, is where the acf_json write the local json file.
* So this hook has to be 9, just before acf write the file.
*/
add_action('acf/update_field_group', 'maybe_switch_json_location_just_before_saving', 9);
add_action('acf/duplicate_field_group', 'maybe_switch_json_location_just_before_saving', 9);
add_action('acf/untrash_field_group', 'maybe_switch_json_location_just_before_saving', 9);
add_action('acf/trash_field_group', 'maybe_switch_json_location_just_before_saving', 9);
add_action('acf/delete_field_group', 'maybe_switch_json_location_just_before_saving', 9);
function maybe_switch_json_location_just_before_saving($field_group) {
if ($field_group['is_acf_component']) {
acf_update_setting('save_json', get_stylesheet_directory() . '/acf-json/components/');
}
}
/**
* In /includes/json.php:17, is where the acf_json write the local json file.
* So this hook has to be 11, just after acf finished writing the file.
*/
add_action('acf/update_field_group', 'switch_json_location_back_after_saving', 11);
add_action('acf/duplicate_field_group', 'switch_json_location_back_after_saving', 11);
add_action('acf/untrash_field_group', 'switch_json_location_back_after_saving', 11);
add_action('acf/trash_field_group', 'switch_json_location_back_after_saving', 11);
add_action('acf/delete_field_group', 'switch_json_location_back_after_saving', 11);
function switch_json_location_back_after_saving() {
acf_update_setting('save_json', get_stylesheet_directory() . '/acf-json/');
}
/**
* In case of new install for initial syncing, we need to tell acf to look
* into the component folder as well
*/
add_filter('acf/settings/load_json', 'maybe_load_component_from_json_location');
function maybe_load_component_from_json_location($paths) {
$paths[] = get_stylesheet_directory() . '/acf-json/components/';
return $paths;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment