I try to include multiple php files under one category inside my Wordpress theme and the file/directory structure look like this:
-Theme Folder
--Functions_Folder
---Folder_1
----File1.php
----File2.php
---Folder_2
----File1.php
----File2.php
---Folder_3
----File1.php
----File2.php
I want to include all those sub functions into my theme function.php and I used this code:
foreach(glob(get_template_directory() . "/Functions_Folder/*.php") as $file){
require $file;
}
Its working fine with the *.php files located inside the parent folder --Functions_Folder.
Is there any function can call all the files only inside the --Functions_Folder including all the sub folders *.php files?
Thank you,
I try to include multiple php files under one category inside my Wordpress theme and the file/directory structure look like this:
-Theme Folder
--Functions_Folder
---Folder_1
----File1.php
----File2.php
---Folder_2
----File1.php
----File2.php
---Folder_3
----File1.php
----File2.php
I want to include all those sub functions into my theme function.php and I used this code:
foreach(glob(get_template_directory() . "/Functions_Folder/*.php") as $file){
require $file;
}
Its working fine with the *.php files located inside the parent folder --Functions_Folder.
Is there any function can call all the files only inside the --Functions_Folder including all the sub folders *.php files?
Thank you,
Share Improve this question edited Jul 17, 2017 at 21:30 Tariq Ahmed asked Jul 17, 2017 at 21:24 Tariq AhmedTariq Ahmed 1682 silver badges6 bronze badges1 Answer
Reset to default 0You need to use some recursion on the function:
function require_all_files($dir) {
foreach( glob( "$dir/*" ) as $path ){
if ( preg_match( '/\.php$/', $path ) ) {
require_once $path; // it's a PHP file so just require it
} elseif ( is_dir( $path ) ) {
require_all_files( $path ); // it's a subdir, so call the same function for this subdir
}
}
}
require_all_files( get_template_directory() . "/Functions_Folder" );