wppaste
WordPress

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:1463
Scans a directory for files of a certain extension.

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 $path directory 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

Reads or writes the filesystem via is_dir().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
9–33

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 75.

Plugin surface
1 hook

Third-party callbacks on 'theme_scandir_exclusions' run inside this call, and their cost is not bounded by anything here.

Called by
3

3 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessis_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.

WhenInstructionsCalls it makes
!is_dir()9is_dir()
is_dir()28–33is_dir(), trailingslashit(), scandir(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev759–3311
8.5759–3311
8.4759–331110 fewer instructions than PHP 8.3
8.3859–3711
8.2859–3711
8.1859–3711
7.4859–3711

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:

  1. apply_filters( theme_scandir_exclusions )filterline 1488 (+25 into the body)

    Filters the array of excluded directories and files while scanning theme folder.

Uses · 3

Used by · 3

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 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.