Title: WP_Theme::scandir
Published: April 25, 2014
Last modified: May 20, 2026

---

# WP_Theme::scandir( string $path, array|string|null $extensions = null, int $depth, string $relative_path = '' ): string[]|false

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Scans a directory for files of a certain extension.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#parameters)󠁿

 `$path`stringrequired

Absolute path to search.

`$extensions`array|string|nulloptional

Array of extensions to find, string of a single extension, or null for all extensions.

Default:`null`

`$depth`intoptional

How many levels deep to search for files. Accepts 0, 1+, or -1 (infinite depth).
Default 0.

`$relative_path`stringoptional

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:`''`

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#return)󠁿

 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.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#source)󠁿

    ```php
    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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-theme.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/class-wp-theme.php#L1463)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/class-wp-theme.php#L1463-L1506)

## 󠀁[Hooks](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#hooks)󠁿

 [apply_filters( ‘theme_scandir_exclusions’, string[] $exclusions )](https://developer.wordpress.org/reference/hooks/theme_scandir_exclusions/)

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

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Theme::scandir()](https://developer.wordpress.org/reference/classes/wp_theme/scandir/)`wp-includes/class-wp-theme.php` |

Scans a directory for files of a certain extension.

  | 
| [trailingslashit()](https://developer.wordpress.org/reference/functions/trailingslashit/)`wp-includes/formatting.php` |

Appends a trailing slash.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 1 more](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#)

| Used by | Description | 
| [WP_Theme::get_block_patterns()](https://developer.wordpress.org/reference/classes/wp_theme/get_block_patterns/)`wp-includes/class-wp-theme.php` |

Gets block pattern data for a specified theme.

  | 
| [WP_Theme::get_files()](https://developer.wordpress.org/reference/classes/wp_theme/get_files/)`wp-includes/class-wp-theme.php` |

Returns files in the theme’s directory.

  | 
| [WP_Theme::scandir()](https://developer.wordpress.org/reference/classes/wp_theme/scandir/)`wp-includes/class-wp-theme.php` |

Scans a directory for files of a certain extension.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_theme/scandir/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.4.0](https://developer.wordpress.org/reference/since/3.4.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_theme%2Fscandir%2F)
before being able to contribute a note or feedback.