wppaste
WordPress

WP_Theme::get_block_patterns(): array

Since
6.4.0
Source
wp-includes/class-wp-theme.php:1856
Gets block pattern data for a specified theme.

Description

Each pattern is defined as a PHP file and defines its metadata using plugin-style headers. The minimum required definition is:

/**
 * Title: My Pattern
 * Slug: my-theme/my-pattern
 *

The output of the PHP source corresponds to the content of the pattern, e.g.:

<main><p><?php echo "Hello"; ?></p></main>

If applicable, this will collect from both parent and child theme.

Other settable fields include:

- Description
- Viewport Width
- Inserter (yes/no)
- Categories (comma-separated values)
- Keywords (comma-separated values)
- Block Types (comma-separated values)
- Post Types (comma-separated values)
- Template Types (comma-separated values)

Compatibility

WordPress
since 6.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.

Return value

array
Block pattern data.

Performance profile

How much work a call to WP_Theme::get_block_patterns() 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 file_exists().

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
11–46

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

Plugin surface
1 hook

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

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • filesystemfilesystem accessfile_exists()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 9 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Theme::get_block_patterns() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_array($pattern_data) && !$value11wp_is_development_mode(), ->get_pattern_cache()
!is_array($pattern_data) && !file_exists() && $value19wp_is_development_mode(), ->get_pattern_cache(), ->get_stylesheet_directory(), file_exists()
!is_array($pattern_data) && !file_exists() && !$value22wp_is_development_mode(), ->get_pattern_cache(), ->get_stylesheet_directory(), file_exists(), ->set_pattern_cache()
is_array($pattern_data) && $value && !file_exists()22wp_is_development_mode(), ->get_pattern_cache(), ->delete_pattern_cache(), ->get_stylesheet_directory(), file_exists()
is_array($pattern_data) && !file_exists()25wp_is_development_mode(), ->get_pattern_cache(), ->delete_pattern_cache(), ->get_stylesheet_directory(), file_exists(), ->set_pattern_cache()
!is_array($pattern_data) && file_exists() && $value36–40wp_is_development_mode(), ->get_pattern_cache(), ->get_stylesheet_directory(), file_exists(), ::scandir(), apply_filters(), trailingslashit()
!is_array($pattern_data) && file_exists() && !$value39–43wp_is_development_mode(), ->get_pattern_cache(), ->get_stylesheet_directory(), file_exists(), ::scandir(), apply_filters(), trailingslashit(), ->set_pattern_cache()
is_array($pattern_data) && $value && file_exists()39–43wp_is_development_mode(), ->get_pattern_cache(), ->delete_pattern_cache(), ->get_stylesheet_directory(), file_exists(), ::scandir(), apply_filters(), trailingslashit()
is_array($pattern_data) && file_exists()42–46wp_is_development_mode(), ->get_pattern_cache(), ->delete_pattern_cache(), ->get_stylesheet_directory(), file_exists(), ::scandir(), apply_filters(), trailingslashit(), ->set_pattern_cache()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev15211–4617
8.515211–4617
8.415211–46176 fewer instructions than PHP 8.3
8.315811–4617
8.215811–4617
8.115811–46171 fewer instruction than PHP 7.4
7.415911–4617

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::get_block_patterns() runs, in this order:

  1. apply_filters( theme_block_pattern_files )filterline 1888 (+32 into the body)

    Filters list of block pattern files for a theme.

Uses · 12

Source code

	public function get_block_patterns() {		$can_use_cached = ! wp_is_development_mode( 'theme' ); 		$pattern_data = $this->get_pattern_cache();		if ( is_array( $pattern_data ) ) {			if ( $can_use_cached ) {				return $pattern_data;			}			// If in development mode, clear pattern cache.			$this->delete_pattern_cache();		} 		$dirpath      = $this->get_stylesheet_directory() . '/patterns';		$pattern_data = array(); 		if ( ! file_exists( $dirpath ) ) {			if ( $can_use_cached ) {				$this->set_pattern_cache( $pattern_data );			}			return $pattern_data;		} 		$files = (array) self::scandir( $dirpath, 'php', -1 ); 		/**		 * Filters list of block pattern files for a theme.		 *		 * @since 6.8.0		 *		 * @param array  $files   Array of theme files found within `patterns` directory.		 * @param string $dirpath Path of theme `patterns` directory being scanned.		 */		$files = apply_filters( 'theme_block_pattern_files', $files, $dirpath ); 		$dirpath = trailingslashit( $dirpath ); 		if ( ! $files ) {			if ( $can_use_cached ) {				$this->set_pattern_cache( $pattern_data );			}			return $pattern_data;		} 		$default_headers = array(			'title'         => 'Title',			'slug'          => 'Slug',			'description'   => 'Description',			'viewportWidth' => 'Viewport Width',			'inserter'      => 'Inserter',			'categories'    => 'Categories',			'keywords'      => 'Keywords',			'blockTypes'    => 'Block Types',			'postTypes'     => 'Post Types',			'templateTypes' => 'Template Types',		); 		$properties_to_parse = array(			'categories',			'keywords',			'blockTypes',			'postTypes',			'templateTypes',		); 		foreach ( $files as $file ) {			$pattern = get_file_data( $file, $default_headers ); 			if ( empty( $pattern['slug'] ) ) {				_doing_it_wrong(					__FUNCTION__,					sprintf(						/* translators: 1: file name. */						__( 'Could not register file "%s" as a block pattern ("Slug" field missing)' ),						$file					),					'6.0.0'				);				continue;			}

Changelog

Introduced in 6.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.1.0 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.