I think the problem is with your relative path. When you visit the /wp-admin/
, it will try to locate the wp-content/themes/vac3/acf
within and it doesn’t find it there. Try instead the full path with help of __DIR__
or get_template_directory()
– more details in this blog post. What if you later remove the acf/
subdirectory? You might want to check it’s existence with is_dir()
. Also you might want to use the FilesystemIterator
(the parent class) constants and use FilesystemIterator::SKIP_DOTS
to skip .
and ..
pointers and FilesystemIterator::FOLLOW_SYMLINKS
(PHP v5.3.1+) to … follow symlinks. You might also want to use FilesystemIterator::CURRENT_AS_PATHNAME
to avoid returning objects when you just need the path to files.
If you don’t need this on the backend you could hook it to the front-end or use the ! is_admin()
check within your hook. You should also take care of only loading this where needed. I also wonder if this should be a plugin? Then there are alternatives like autoloading.
The normal thing to do is to wrap everything up in a callback attached to some filter or hook. Example:
// Use the proper filter – below just as example
add_filter( 'wp_loaded', function() {
// Only serve for public requests
if ( is_admin() )
return;
$Iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( get_template_directory().'/acf' )
);
$Regex = new RegexIterator(
$Iterator,
'/^.+\.php$/i',
RecursiveRegexIterator::GET_MATCH
);
foreach ( $Regex as $file )
require_once $file[0];
} );
Even better would be if you just use the FilesystemIterator
in case you only have a single folder to require files from – non recursively:
add_action( 'wp_loaded', function() {
if ( is_admin() )
return;
$files = new \FilesystemIterator(
get_template_directory().'/acf',
\FilesystemIterator::SKIP_DOTS
| FilesystemIterator::FOLLOW_SYMLINKS
| FilesystemIterator::CURRENT_AS_PATHNAME
);
foreach ( $files as $file )
{
/** @noinspection PhpIncludeInspection */
! $files->isDir() and include $files->getRealPath();
}
} );