wordpress - PHP scandir - multiple directories -


i creating wordpress plugin allows user apply sorting rules particular template (page, archive, single etc). populating list of pages using php scandir so:

$files = scandir(get_template_directory()); 

the problem keep single.php templates in '/single' subfolder these templates not being called above function.

how can use multiple directories within scandir function (perhaps array?) or need different solution?

so trying to:

$files  =   scandir( get_template_directory() , get_template_directory().'/single' ); 

my current solution (not elegant requires 2 each loops):

        function query_caller_is_template_file_get_template_files()             {                 $template_files_list    =   array();                  $files          =   scandir(get_template_directory());                 $singlefiles    =   scandir(get_template_directory().'/single');                  foreach($files   $file)                     {                         if(strpos($file, '.php')    === false)                             continue;                          $template_files_list[]  =   $file;                     }                  foreach($singlefiles   $singlefile)                     {                         if(strpos($file, '.php')    === false)                             continue;                          $template_files_list[]  =   $singlefile;                     }                  return $template_files_list;             } 

first, there's not wrong you're doing. have 2 directories, same thing twice. of course make little cleaner , avoid blatant copy paste:

$files = array_merge(     scandir(get_template_directory()),     scandir(get_template_directory().'/single') ); 

now iterate on single array.

in case, getting file list recursively doesn't make sense, there may subdirectories don't want check. if did want recurse subdirectories, opendir() , readdir() along is_dir() allow build recursive scan function.

you event tighten '.php' filter part bit array_filter().

$files = array_filter($files, function($file){     return strpos($file, '.php'); }); 

here i'm assuming should file start .php you're not interested in making list (as strpos() return falsy value of 0 in case). i'm assuming you're sure there no files have .php in middle somewhere.

like, template.php.bak, because you'll using version control that.

if there chance of that, may want tighten check bit ensure .php @ end of filename.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -