I try to include multiple php files under one category inside my Wordpress theme and the file/directory structure look like this:

-Theme Folder

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,

1 Answer 1


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
    Commented Jul 21, 2017 at 22:59
  • Thank you for your help, but this did not work with me. Commented Jul 23, 2017 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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