Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishterk/b25e7c045c85e9591a9c596060494c8c to your computer and use it in GitHub Desktop.
Save mishterk/b25e7c045c85e9591a9c596060494c8c to your computer and use it in GitHub Desktop.
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