wppaste
WordPress

WP_Theme::get_block_patterns(): array

Since
6.4.0
Source
wp-includes/class-wp-theme.php:1839
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.10
  • 6.8.11
  • 6.9.10
  • 7.0.7
  • 7.1.3

Present in every tracked release (6.7.10 to 7.1.3), 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–35

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

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

What it touches

  • filesystemfilesystem accessfile_exists()called directly
  • hookthird-party callbacksapply_filters()one call below WP_Theme::get_block_patterns()

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() && $value25–29wp_is_development_mode(), ->get_pattern_cache(), ->get_stylesheet_directory(), file_exists(), glob()
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() && !$value28–32wp_is_development_mode(), ->get_pattern_cache(), ->get_stylesheet_directory(), file_exists(), glob(), ->set_pattern_cache()
is_array($pattern_data) && $value && file_exists()28–32wp_is_development_mode(), ->get_pattern_cache(), ->delete_pattern_cache(), ->get_stylesheet_directory(), file_exists(), glob()
is_array($pattern_data) && file_exists()31–35wp_is_development_mode(), ->get_pattern_cache(), ->delete_pattern_cache(), ->get_stylesheet_directory(), file_exists(), glob(), ->set_pattern_cache()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14111–3517
8.514111–3517
8.414111–35176 fewer instructions than PHP 8.3
8.314711–3517
8.214711–3517
8.114711–35171 fewer instruction than PHP 7.4
7.414811–3517

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.

Uses · 9

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 = glob( $dirpath . '*.php' );		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;			} 			if ( ! preg_match( '/^[A-z0-9\/_-]+$/', $pattern['slug'] ) ) {				_doing_it_wrong(					__FUNCTION__,					sprintf(						/* translators: 1: file name; 2: slug value found. */						__( 'Could not register file "%1$s" as a block pattern (invalid slug "%2$s")' ),						$file,						$pattern['slug']					),					'6.0.0'				);			} 			// Title is a required property.

Changelog

Introduced in 6.4.0. Unchanged from 6.7.10 through 7.1.3.

  1. 6.7.10
  2. 6.8.11
  3. 6.9.10
  4. 7.0.7
  5. 7.1.3

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

About this page

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