Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Define custom load points for Advanced Custom Fields (ACF) JSON files. For more info see https://philkurth.com.au/tips/how-to-get-advanced-custom-fields-to-load-json-files-from-more-than-one-directory/
<?php
add_filter( 'acf/settings/load_json', function ( array $directories ) {
$base_dir = get_stylesheet_directory() . '/components';
$resource = opendir( $base_dir );
while ( ( $file = readdir( $resource ) ) !== false ) {
// Skip current and parent directory references
if ( in_array( $file, array( '.', '..' ), true ) ) {
continue;
}
// Get the full path
$dir = "$base_dir/$file";
// If we have a directory (not a file) add it to our ACF JSON load paths
if ( is_dir( $dir ) ) {
$directories[] = $dir;
}
}
closedir( $resource );
return $directories;
} );
<?php
add_filter( 'acf/settings/load_json', function ( array $directories ) {
// Define as many directories as necessary. ACF will look in each
// directory and load any JSON files it finds.
$directories[] = get_stylesheet_directory() . '/components/one';
$directories[] = get_stylesheet_directory() . '/components/two';
$directories[] = get_stylesheet_directory() . '/components/three';
return $directories;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.