1

I'd like to scan a plugin's directory for files with specific extensions, like php, css and js.

I know there is scandir() function in WP_Theme class that's perfect for this work. The problem is that it's declared private so I can't use it outside of the class.

Any advice what's the best way to do this? Is there another native WP function that can be used instead?

Many thanks, Dasha

4
  • 2
    how about… scandir – Milo Jan 8 '14 at 18:22
  • @Milo, I couldn't figure out how to use specific file extensions with PHP's scandir. Is it possible? – dashaluna Jan 8 '14 at 18:26
  • check the returned array for specific file types, or use glob with a pattern. – Milo Jan 8 '14 at 18:33
  • 1
    Just looking into glob function.. I don't think it scans the nested folders. I'm more tempted to copy over the WP_Theme::scandir() function. Unless, there is some other similar function that WP provides, that does most of the work? – dashaluna Jan 8 '14 at 19:25
4

You can use PHP5 RecursiveDirectoryIterator with RecursiveIteratorIterator

$directory = '/project_root/wp-content/plugins/your-plugin'; //Your plugin dir

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));

    while ($it->valid()) { //Check the file exist
    if (!$it->isDot()) { //if not parent ".." or current "."
            if (strpos($it->key(), '.php') !== false
               || strpos($it->key(), '.css') !== false
               || strpos($it->key(), '.js') !== false
            ) {
                //Do your stuff with matched extension files
                echo $it->key() . '<br>'; //output: main.js, index.php, style.css etc....
            }
        }
    }
1

As of WP 3.4 there is a public get_files() method. You can create a WP_Theme instance with the plugin dir set as theme root.

// get theme instance of WP_Theme
$current_dir = new WP_Theme('my-plugin-dir',WP_PLUGIN_DIR);

// get js and css files up to 10 subfolders depth
$files = $current_dir->get_files( array('js','css') , 10 );

There isn't very much documentation on it in the WP-Codex Page, so having a look at the source code might be more instructive.

0

Thank you everyone for the inputs and advice.

I've decided to copy over the WP_Theme::scandir() function itself. I understand what's going on there and there is no other native WP function to scan directories for files with specific extensions (with an option scanning sub-folders).

P.S. I could not figure out how to link to WP code. Here is the copy of the WP_Theme::scandir() as of 13 Jan 2014

/**
 * Scans a directory for files of a certain extension.
 *
 * @since 3.4.0
 * @access private
 *
 * @param string $path Absolute path to search.
 * @param mixed  Array of extensions to find, string of a single extension, or null for all extensions.
 * @param int $depth How deep to search for files. Optional, defaults to a flat scan (0 depth). -1 depth is infinite.
 * @param string $relative_path The basename of the absolute path. Used to control the returned path
 *  for the found files, particularly when this function recurses to lower depths.
 */
private static function scandir( $path, $extensions = null, $depth = 0, $relative_path = '' ) {
    if ( ! is_dir( $path ) )
        return false;

    if ( $extensions ) {
        $extensions = (array) $extensions;
        $_extensions = implode( '|', $extensions );
    }

    $relative_path = trailingslashit( $relative_path );
    if ( '/' == $relative_path )
        $relative_path = '';

    $results = scandir( $path );
    $files = array();

    foreach ( $results as $result ) {
        if ( '.' == $result[0] )
            continue;
        if ( is_dir( $path . '/' . $result ) ) {
            if ( ! $depth || 'CVS' == $result )
                continue;
            $found = self::scandir( $path . '/' . $result, $extensions, $depth - 1 , $relative_path . $result );
            $files = array_merge_recursive( $files, $found );
        } elseif ( ! $extensions || preg_match( '~\.(' . $_extensions . ')$~', $result ) ) {
            $files[ $relative_path . $result ] = $path . '/' . $result;
        }
    }

    return $files;
}
4
  • It is not easier to scan via glob, like $files = glob( "folder_path/*.php" );? – bueltge Jan 13 '14 at 16:19
  • Have you looked at my answer? didn't it help? – Rahil Wazir Jan 21 '14 at 20:40
  • @RahilWazir, thank you for taking time and helping. The only reason I went for WP function is because it's already exists with all the functionality that I wanted (specific extensions, sub-folder lookup). And I understand what's going on there a bit more. – dashaluna Jan 22 '14 at 17:07
  • 2
    Rahils answer uses a recursive directory iterator, aka it recurses down through the folders into sub and sub sub and sub sub sub folders etc. It does everything you need it to do, and perhaps it may even be faster ( its certainly simpler ), for that it deserves an up vote at least – Tom J Nowell Apr 1 '14 at 12:24

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.