admin管理员组文章数量:1393592
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" );
本文标签: theme developmentHow to include all files within a folder amp its sub folders to functionsphp
版权声明:本文标题:theme development - How to include all files within a folder & its sub folders to functions.php? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744764224a2623940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论