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,

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" );
  • Did this helped? – dbeja Jul 21 '17 at 22:59
  • Thank you for your help, but this did not work with me. – Tariq Ahmed Jul 23 '17 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.