Home › Forums › General Issues › Load fields from PHP exported file by default
Hey,
By default I want to load all fields from the PHP exported file. Because manual synchronization of the updated fields by the user of my themes is too cumbersome. Usually I have a child theme, which is loading the fields from the PHP exported file by default.
This is included in the child theme functions.php and it works fine:
include_once( get_template_directory() . '/inc/acf-field-groups.php' );
BUT, not everyone can read the documentation to find out who should work with the child theme. The reality is, a lot of theme user work with the main theme instead of the child theme.
So I thought I can add a field loading switch like this:
if( ! get_field('mode_custom_fields_json', 'option') ) {
include_once( get_stylesheet_directory() . '/inc/acf-field-groups.php' );
}
The user must first to activate the field loading from JSON manually. It’s the best way to keep users from manually syncing the fields.
BUT if I added this code above to the main theme functions.php and it dosen’t work 🙁
Without the condition if( ! get_field('mode_custom_fields_json', 'option') )
, the fields will loaded from the PHP exported file by default and it works.
Why this is not working with the if condition?
Ok, I have solved this issue by myself. The reason why the PHP exported file is not loading inside the if condition, I have loaded the JSON files at the same time with the following code:
if ( ! function_exists( 'acf_json_load_point' ) ) :
function acf_json_load_point( $paths ) {
unset($paths[0]); // remove original path (optional)
$paths[] = get_stylesheet_directory() . '/inc/acf-json'; // append path
return $paths;
}
endif;
add_filter('acf/settings/load_json', 'acf_json_load_point');
If I load the “PHP exported file” OR the “JSON files” only, handled by a if condition, everything is fine and it works great 🙂
Here is my solution:
/*****************************************************************/
/* LOADING SWITCH FOR ACF FIELD GROUPS FROM PHP OR JSON FILE */
/*****************************************************************/
add_filter('acf/settings/load_json', function( $paths ) {
$dev_mode = get_field('mode_custom_fields_json', 'option');
// load basic [ MAIN THEME ] field groups from the [ MAIN THEME JSON ] folder
$paths = array( get_template_directory() . '/inc/acf-json' );
// load custom additional [ CHILD THEME ] field groups from the [ CHILD THEME JSON ] folder
if( is_child_theme() && $dev_mode ) {
$paths[] = get_stylesheet_directory() . '/acf-json';
}
// WITHOUT ACTIVATED DEV MODE --> load all fields from PHP file by the [ MAIN THEME ]
if( ! $dev_mode ) {
// load all field groups from PHP exported file for theme users
include_once( get_template_directory() . '/inc/acf-field-groups.php' );
}
return $paths;
});
The topic ‘Load fields from PHP exported file by default’ is closed to new replies.
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.