My functions.php includes other function files located within a 'functions' directory.

Currently they're individually added, in the format of this example:


How can I modify this to include all files within the 'functions' directory, without listing them individually?


You can include/require all *.php files recursively using following function.

foreach(glob(get_template_directory() . "/*.php") as $file){
    require $file;

Alternatively You can use following function as-well.

$Directory = new RecursiveDirectoryIterator(get_template_directory().'functions/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);

foreach($Regex as $yourfiles) {
    include $yourfiles->getPathname();

P.S Got the solution From Here.

| improve this answer | |
  • @ JItendra Rana ... Thanks. can this be modified to include files from sub-directories inside the 'functions' directory? – glvr May 25 '17 at 9:38
  • Do you want to include all the files recursively in all sub-directories ? – JItendra Rana May 25 '17 at 10:00
  • Updated the answer to more elegant solution for recursive directories. Please check – JItendra Rana May 25 '17 at 10:14
  • @ JItendra Rana ... Thanks. I'm getting various errors with the updated code, so will instead use your previous suggestion, which I think was: foreach(glob('/add_path_to_theme/wp-content/themes/theme-name/functions/*.php') as $file) {include $file;} If that is your code, it might be worth re-adding it to your update so others can access it. – glvr May 30 '17 at 7:29
  • Updated the answer. Pls Check – JItendra Rana May 30 '17 at 7:48

Here's how I did it from my functions.php file in WordPress:

 * Functions
 * Require all PHP files in the /functions/ directory
foreach (glob(get_template_directory() . "/functions/*.php") as $function) {
    $function= basename($function);
    require get_template_directory() . '/functions/' . $function;
| improve this answer | |

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.