_register_remote_theme_patterns()WP 6.0.0

Registers patterns from Pattern Directory provided by a theme's theme.json file.

This is an internal function for using it by WP core itself. It's not recommended to use this function in your code.

Hooks from the function

Return

null. Nothing.

Usage

_register_remote_theme_patterns();

Changelog

Since 6.0.0 Introduced.

Code of _register_remote_theme_patterns() WP 6.0

function _register_remote_theme_patterns() {
	if ( ! get_theme_support( 'core-block-patterns' ) ) {
		return;
	}

	/** This filter is documented in wp-includes/block-patterns.php */
	if ( ! apply_filters( 'should_load_remote_block_patterns', true ) ) {
		return;
	}

	if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) {
		return;
	}

	$pattern_settings = WP_Theme_JSON_Resolver::get_theme_data()->get_patterns();
	if ( empty( $pattern_settings ) ) {
		return;
	}

	$request         = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
	$request['slug'] = $pattern_settings;
	$response        = rest_do_request( $request );
	if ( $response->is_error() ) {
		return;
	}
	$patterns          = $response->get_data();
	$patterns_registry = WP_Block_Patterns_Registry::get_instance();
	foreach ( $patterns as $pattern ) {
		$pattern_name = sanitize_title( $pattern['title'] );
		// Some patterns might be already registered as core patterns with the `core` prefix.
		$is_registered = $patterns_registry->is_registered( $pattern_name ) || $patterns_registry->is_registered( "core/$pattern_name" );
		if ( ! $is_registered ) {
			register_block_pattern( $pattern_name, (array) $pattern );
		}
	}
}