Title: _register_theme_block_patterns
Published: May 25, 2022
Last modified: August 20, 2026

---

# _register_theme_block_patterns()

## In this article

 * [Source](https://developer.wordpress.org/reference/functions/_register_theme_block_patterns/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/_register_theme_block_patterns/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_register_theme_block_patterns/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/_register_theme_block_patterns/?output_format=md#wp--skip-link--target)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

Register any patterns that the active theme may provide under its `./patterns/` 
directory.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/_register_theme_block_patterns/?output_format=md#source)󠁿

    ```php
    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 );
    		}
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/block-patterns.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/block-patterns.php#L414)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/block-patterns.php#L414-L477)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_register_theme_block_patterns/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Block_Patterns_Registry::get_instance()](https://developer.wordpress.org/reference/classes/wp_block_patterns_registry/get_instance/)`wp-includes/class-wp-block-patterns-registry.php` |

Utility method to retrieve the main instance of the class.

  | 
| [register_block_pattern()](https://developer.wordpress.org/reference/functions/register_block_pattern/)`wp-includes/block-patterns.php` |

Registers a new block pattern.

  | 
| [wp_get_active_and_valid_themes()](https://developer.wordpress.org/reference/functions/wp_get_active_and_valid_themes/)`wp-includes/load.php` |

Retrieves an array of active and valid themes.

  | 
| [translate_with_gettext_context()](https://developer.wordpress.org/reference/functions/translate_with_gettext_context/)`wp-includes/l10n.php` |

Retrieves the translation of $text in the context defined in $context.

  | 
| [wp_get_theme()](https://developer.wordpress.org/reference/functions/wp_get_theme/)`wp-includes/theme.php` |

Gets a [WP_Theme](https://developer.wordpress.org/reference/classes/wp_theme/) object for a theme.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [_doing_it_wrong()](https://developer.wordpress.org/reference/functions/_doing_it_wrong/)`wp-includes/functions.php` |

Marks something as being incorrectly called.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/_register_theme_block_patterns/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/_register_theme_block_patterns/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/_register_theme_block_patterns/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.4.0](https://developer.wordpress.org/reference/since/6.4.0/) | Uses the `WP_Theme::get_block_patterns` method. | 
| [6.2.0](https://developer.wordpress.org/reference/since/6.2.0/) | The `templateTypes` property was added. | 
| [6.1.0](https://developer.wordpress.org/reference/since/6.1.0/) | The `postTypes` property was added. | 
| [6.0.0](https://developer.wordpress.org/reference/since/6.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_register_theme_block_patterns%2F)
before being able to contribute a note or feedback.