WP_Theme::get_files( mixed $type = null, int $depth, bool $search_parent = false )

Return files in the theme’s directory.


Description Description


Parameters Parameters

$type

(mixed) (Optional) Array of extensions to return. Defaults to all files (null).

Default value: null

$depth

(int) (Optional) How deep to search for files. Defaults to a flat scan (0 depth). -1 depth is infinite.

$search_parent

(bool) (Optional) Whether to return parent files. Defaults to false.

Default value: false


Top ↑

Return Return

(array) Array of files, keyed by the path to the file relative to the theme's directory, with the values being absolute paths.


Top ↑

Source Source

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

	public function get_files( $type = null, $depth = 0, $search_parent = false ) {
		// get and cache all theme files to start with.
		$label = sanitize_key( 'files_' . $this->cache_hash . '-' . $this->get( 'Version' ) );
		$transient_key = substr( $label, 0, 29 ) . md5( $label );

		$all_files = get_transient( $transient_key );
		if ( false === $all_files ) {
			$all_files = (array) self::scandir( $this->get_stylesheet_directory(), null, -1 );

			if ( $search_parent && $this->parent() ) {
				$all_files += (array) self::scandir( $this->get_template_directory(), null, -1 );
			}

			set_transient( $transient_key, $all_files, HOUR_IN_SECONDS );
		}

		// Filter $all_files by $type & $depth.
		$files = array();
		if ( $type ) {
			$type = (array) $type;
			$_extensions = implode( '|', $type );
		}
		foreach ( $all_files as $key => $file ) {
			if ( $depth >= 0 && substr_count( $key, '/' ) > $depth ) {
				continue; // Filter by depth.
			}
			if ( ! $type || preg_match( '~\.(' . $_extensions . ')$~', $file ) ) { // Filter by type.
				$files[ $key ] = $file;
			}
		}

		return $files;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.4.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.