WP_Theme::scandir( string $path, array|string|null $extensions = null, int $depth = 0, string $relative_path = '' ): string[]|false
- Since
- 3.4.0
- Source
wp-includes/class-wp-theme.php:1455
Compatibility
- WordPress
- since 3.4.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$pathstring- Absolute path to search.
$extensionsarray|string|nulloptional- Array of extensions to find, string of a single extension, or null for all extensions. Default null.Default:
null $depthintoptional- How many levels deep to search for files. Accepts 0, 1+, or -1 (infinite depth). Default 0.Default:
0 $relative_pathstringoptional- The basename of the absolute path. Used to control the returned path for the found files, particularly when this function recurses to lower depths. Default empty.Default:
''
Return value
string[]|false- Array of files, keyed by the path to the file relative to the
$pathdirectory prepended with$relative_path, with the values being absolute paths. False otherwise.
Performance profile
How much work a call to WP_Theme::scandir() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 9–33
- Plugin surface
- 1 hook
- Called by
- 2
Reads or writes the filesystem via is_dir().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 75.
Third-party callbacks on 'theme_scandir_exclusions' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
is_dir()called directly
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Theme::scandir() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_dir() | 9 | is_dir() |
is_dir() | 28–33 | is_dir(), trailingslashit(), scandir(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 75 | 9–33 | 11 | |
| 8.5 | 75 | 9–33 | 11 | |
| 8.4 | 75 | 9–33 | 11 | 10 fewer instructions than PHP 8.3 |
| 8.3 | 85 | 9–37 | 11 | |
| 8.2 | 85 | 9–37 | 11 | |
| 8.1 | 85 | 9–37 | 11 | |
| 7.4 | 85 | 9–37 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while WP_Theme::scandir() runs, in this order:
- apply_filters( theme_scandir_exclusions )filterline 1480 (+25 into the body)
Filters the array of excluded directories and files while scanning theme folder.
Uses · 3
- trailingslashit()Appends a trailing slash.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_Theme::scandir()Scans a directory for files of a certain extension.
Used by · 2
- WP_Theme::get_files()Returns files in the theme's directory.
- WP_Theme::scandir()Scans a directory for files of a certain extension.
Source code
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(); /** * Filters the array of excluded directories and files while scanning theme folder. * * @since 4.7.4 * * @param string[] $exclusions Array of excluded directories and files. */ $exclusions = (array) apply_filters( 'theme_scandir_exclusions', array( 'CVS', 'node_modules', 'vendor', 'bower_components' ) ); foreach ( $results as $result ) { if ( '.' === $result[0] || in_array( $result, $exclusions, true ) ) { continue; } if ( is_dir( $path . '/' . $result ) ) { if ( ! $depth ) { 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; }Changelog
Introduced in 3.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/class-wp-theme.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.