最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - How to include all files within a folder & its sub folders to functions.php?

programmeradmin2浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

You 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" );
发布评论

评论列表(0)

  1. 暂无评论