WP_Theme::get_block_patterns(): array
- Since
- 6.4.0
- Source
wp-includes/class-wp-theme.php:1856
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
- Scaling
- Scales with input
- Instructions
- 11–46
- Plugin surface
- 1 hook
- Called by
- 0
Reads or writes the filesystem via file_exists().
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 152.
Third-party callbacks on 'theme_block_pattern_files' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
file_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.
| When | Instructions | Calls it makes |
|---|---|---|
is_array($pattern_data) && !$value | 11 | wp_is_development_mode(), ->get_pattern_cache() |
!is_array($pattern_data) && !file_exists() && $value | 19 | wp_is_development_mode(), ->get_pattern_cache(), ->get_stylesheet_directory(), file_exists() |
!is_array($pattern_data) && !file_exists() && !$value | 22 | wp_is_development_mode(), ->get_pattern_cache(), ->get_stylesheet_directory(), file_exists(), ->set_pattern_cache() |
is_array($pattern_data) && $value && !file_exists() | 22 | wp_is_development_mode(), ->get_pattern_cache(), ->delete_pattern_cache(), ->get_stylesheet_directory(), file_exists() |
is_array($pattern_data) && !file_exists() | 25 | wp_is_development_mode(), ->get_pattern_cache(), ->delete_pattern_cache(), ->get_stylesheet_directory(), file_exists(), ->set_pattern_cache() |
!is_array($pattern_data) && file_exists() && $value | 36–40 | wp_is_development_mode(), ->get_pattern_cache(), ->get_stylesheet_directory(), file_exists(), ::scandir(), apply_filters(), trailingslashit() |
!is_array($pattern_data) && file_exists() && !$value | 39–43 | wp_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–43 | wp_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–46 | wp_is_development_mode(), ->get_pattern_cache(), ->delete_pattern_cache(), ->get_stylesheet_directory(), file_exists(), ::scandir(), apply_filters(), trailingslashit(), ->set_pattern_cache() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 152 | 11–46 | 17 | |
| 8.5 | 152 | 11–46 | 17 | |
| 8.4 | 152 | 11–46 | 17 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 158 | 11–46 | 17 | |
| 8.2 | 158 | 11–46 | 17 | |
| 8.1 | 158 | 11–46 | 17 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 159 | 11–46 | 17 |
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:
- apply_filters( theme_block_pattern_files )filterline 1888 (+32 into the body)
Filters list of block pattern files for a theme.
Uses · 12
- wp_is_development_mode()Checks whether the site is in the given development mode.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- trailingslashit()Appends a trailing slash.
- get_file_data()Retrieves metadata from a file.
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- wp_parse_list()Converts a comma- or space-separated list of scalar values to an array.
- WP_Theme::get_pattern_cache()Gets block pattern cache.
- WP_Theme::delete_pattern_cache()Clears block pattern cache.
- WP_Theme::get_stylesheet_directory()Returns the absolute path to the directory of a theme's "stylesheet" files.
- WP_Theme::set_pattern_cache()Sets block pattern cache.
- WP_Theme::scandir()Scans a directory for files of a certain extension.
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.
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.