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
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
Share Improve this question asked Jan 8, 2014 at 18:11 dashalunadashaluna 2,1024 gold badges33 silver badges45 bronze badges 4 |3 Answers
Reset to default 4You 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....
}
}
}
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.
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;
}
WP_Theme::scandir()
function. Unless, there is some other similar function that WP provides, that does most of the work? – dashaluna Commented Jan 8, 2014 at 19:25