Title: Dynamically populate the theme options array
Last modified: August 19, 2016

---

# Dynamically populate the theme options array

 *  [vestaviascott](https://wordpress.org/support/users/vestaviascott/)
 * (@vestaviascott)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/dynamically-populate-the-theme-options-array/)
 * I’ve created a theme options panel that has a pulldown menu to allow the user
   to select a different stylesheet. Until now, I’ve been hardcoding the stylesheet
   selections, but I now want to dynamically populate them.
 * Since each of my styles has its own images inside my theme’s main images directory,
   I just want to parse this directory and populate my options array with the folder
   name of each folder.
 * I’m sure my method is flawed, but it works up until the point of actually dynamically
   populating the array.
 * Here’s the hardcoded version…
 * “options” => array(“folder one” => “folder1”, “folder two” => “folder2”)),
 * And here’s my dynamic version that almost works…
 * $mydir = getDirectory(‘../wp-content/themes/mytheme/images/’);
 * “options” => array($mydir)),
 *     ```
       function getDirectory( $path = '.', $level = 0 )
       {
       // Directories to ignore when listing output.
       $ignore = array( '.', '..' );
   
       // Open the directory to the handle $dh
       $dh = @opendir( $path );
   
       // Loop through the directory
       while( false !== ( $file = readdir( $dh ) ) )
               {
               // Check that this file is not to be ignored
               if( !in_array( $file, $ignore ) )
                       {
                       // Show directories only
                       if(is_dir( "$path/$file" ) )
                               {
                               // Re-call this same function but on a new directory.
                               // this is what makes function recursive.
                               //echo $file." => ".$file. ", ";
                               // need to return the folders in the form expected by the array. Probably could just add the items directly to the array?
                               $mydir2=$mydir2.'"'.$file.'" => "'.$file. '", ';
                               getDirectory( "$path/$file", ($level+1) );
                       }
               }
       }
       return $mydir2;
       // Close the directory handle
       closedir( $dh );
       }
       ```
   
 * What am I missing???

The topic ‘Dynamically populate the theme options array’ is closed to new replies.

 * 0 replies
 * 1 participant
 * Last reply from: [vestaviascott](https://wordpress.org/support/users/vestaviascott/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/dynamically-populate-the-theme-options-array/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
