DEFINE ('PAGES', '/pages/');
$directory_pages = new DirectoryIterator(PAGES); //ERROR LINE
foreach ($directory_pages as $files) {
if ($files_pages->isFile()) {
$file_name_page = $files_pages->getFilename();
$my_page_content = file_get_contents(PAGES. $file_name_page);
}
}
This code is located in a plugin, where the “pages” folder that I’m trying to access is inside the plugin itself.
So the script is myplugin.php and it resides in the folder “myplugin”, which has a subfolder, “pages”.
I’m trying to load the files in the “pages” directory, but my path reference is throwing an error. Do I need to use ABSPATH here instead?
Or perhaps a dynamic method to get the current directory relative to root of WordPress?
The manual says:
Basically, if
/pagesis not a valid directory, then it won’t work. To be relative to the current working directory you would usepageswithout the leading slash.I’m not familiar with WordPress, but if it supplies a valid base constant prefix to use, then I would prepend that to the path you are using.