_register_theme_block_patterns()
- Since
- 6.0.0, 6.1.0, 6.2.0, 6.4.0
- Source
wp-includes/block-patterns.php:414
./patterns/ directory.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.
Performance profile
How much work a call to _register_theme_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
- Light
- Scaling
- Scales with input
- Instructions
- 4–23
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
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 96.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()one call below _register_theme_block_patterns()
Further down the call graph this can also reach option, cache, serialize, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _register_theme_block_patterns() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!wp_get_active_and_valid_themes() | 4 | wp_get_active_and_valid_themes() |
wp_get_active_and_valid_themes() && !->parent() | 18–19 | wp_get_active_and_valid_themes(), wp_get_theme(), ->parent(), ::WP_Block_Patterns_Registry() |
wp_get_active_and_valid_themes() && ->parent() | 22–23 | wp_get_active_and_valid_themes(), wp_get_theme(), ->parent(), ->parent(), ::WP_Block_Patterns_Registry() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 96 instructions, 4–23 executed per call, 9 branches. The work does not change between versions.
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 · 7
- wp_get_active_and_valid_themes()Retrieves an array of active and valid themes.
- wp_get_theme()Gets a WP_Theme object for a theme.
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- translate_with_gettext_context()Retrieves the translation of $text in the context defined in $context.
- register_block_pattern()Registers a new block pattern.
- WP_Block_Patterns_Registry::get_instance()Utility method to retrieve the main instance of the class.
Source code
function _register_theme_block_patterns() { /* * During the bootstrap process, a check for active and valid themes is run. * If no themes are returned, the theme's functions.php file will not be loaded, * which can lead to errors if patterns expect some variables or constants to * already be set at this point, so bail early if that is the case. */ if ( empty( wp_get_active_and_valid_themes() ) ) { return; } /* * Register patterns for the active theme. If the theme is a child theme, * let it override any patterns from the parent theme that shares the same slug. */ $themes = array(); $theme = wp_get_theme(); $themes[] = $theme; if ( $theme->parent() ) { $themes[] = $theme->parent(); } $registry = WP_Block_Patterns_Registry::get_instance(); foreach ( $themes as $theme ) { $patterns = $theme->get_block_patterns(); $dirpath = $theme->get_stylesheet_directory() . '/patterns/'; $text_domain = $theme->get( 'TextDomain' ); foreach ( $patterns as $file => $pattern_data ) { if ( $registry->is_registered( $pattern_data['slug'] ) ) { continue; } $file_path = $dirpath . $file; if ( ! file_exists( $file_path ) ) { _doing_it_wrong( __FUNCTION__, sprintf( /* translators: %s: file name. */ __( 'Could not register file "%s" as a block pattern as the file does not exist.' ), $file ), '6.4.0' ); $theme->delete_pattern_cache(); continue; } $pattern_data['filePath'] = $file_path; // Translate the pattern metadata. // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain,WordPress.WP.I18n.LowLevelTranslationFunction $pattern_data['title'] = translate_with_gettext_context( $pattern_data['title'], 'Pattern title', $text_domain ); if ( ! empty( $pattern_data['description'] ) ) { // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain,WordPress.WP.I18n.LowLevelTranslationFunction $pattern_data['description'] = translate_with_gettext_context( $pattern_data['description'], 'Pattern description', $text_domain ); } register_block_pattern( $pattern_data['slug'], $pattern_data ); } }}Changelog
Introduced in 6.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
WP_Theme::get_block_patterns method.from the docblocktemplateTypes property was added.from the docblockpostTypes property was added.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/block-patterns.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.