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

WP_Theme::scandir() WordPress Method

The WP_Theme::scandir() method is used to scan a directory for themes. It returns an array of file names, which can be used to locate and load the theme files.

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

Scans a directory for files of a certain extension.


Parameters

$path

(string)(Required)Absolute path to search.

$extensions

(array|string|null)(Optional) Array of extensions to find, string of a single extension, or null for all extensions.

Default value: null

$depth

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

$relative_path

(string)(Optional) 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 value: ''


Top ↑

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.


Top ↑

Source

File: wp-includes/class-wp-theme.php

1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
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;
}


Top ↑

Changelog

Changelog
VersionDescription
3.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.