_load_remote_block_patterns()
云策文档标注
概述
_load_remote_block_patterns() 函数用于从 wordpress.org/patterns 注册核心官方区块模式。它通过 REST API 获取模式数据,并调用 register_block_pattern() 进行注册。
关键要点
- 函数参数 $deprecated 已弃用,自 5.9.0 版本起不再使用。
- 通过 get_theme_support('core-block-patterns') 检查主题是否支持核心区块模式。
- 使用 apply_filters('should_load_remote_block_patterns', true) 过滤器控制是否加载远程模式。
- 通过 WP_REST_Request 请求 '/wp/v2/pattern-directory/patterns' 端点,并设置 keyword 参数为 11(代表“core”)。
- 使用 wp_normalize_remote_block_pattern() 规范化模式数据,然后注册为 'core/' 前缀的模式。
代码示例
function _load_remote_block_patterns( $deprecated = null ) {
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '5.9.0' );
$current_screen = $deprecated;
if ( ! $current_screen->is_block_editor ) {
return;
}
}
$supports_core_patterns = get_theme_support( 'core-block-patterns' );
$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
if ( $supports_core_patterns && $should_load_remote ) {
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
$core_keyword_id = 11; // 11 is the ID for "core".
$request->set_param( 'keyword', $core_keyword_id );
$response = rest_do_request( $request );
if ( $response->is_error() ) {
return;
}
$patterns = $response->get_data();
foreach ( $patterns as $pattern ) {
$pattern['source'] = 'pattern-directory/core';
$normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
$pattern_name = 'core/' . sanitize_title( $normalized_pattern['title'] );
register_block_pattern( $pattern_name, $normalized_pattern );
}
}
}注意事项
- 自 5.9.0 版本起,$deprecated 参数已移除,使用时会触发 _deprecated_argument() 警告。
- 模式数据从远程 API 获取,需确保网络连接正常。
- 注册的模式名称以 'core/' 开头,便于识别核心模式。
原文内容
Register Core’s official patterns from wordpress.org/patterns.
Parameters
$deprecatedWP_Screenoptional-
Unused. Formerly the screen that the current request was triggered from.
Default:
null
Source
function _load_remote_block_patterns( $deprecated = null ) {
if ( ! empty( $deprecated ) ) {
_deprecated_argument( __FUNCTION__, '5.9.0' );
$current_screen = $deprecated;
if ( ! $current_screen->is_block_editor ) {
return;
}
}
$supports_core_patterns = get_theme_support( 'core-block-patterns' );
/**
* Filter to disable remote block patterns.
*
* @since 5.8.0
*
* @param bool $should_load_remote
*/
$should_load_remote = apply_filters( 'should_load_remote_block_patterns', true );
if ( $supports_core_patterns && $should_load_remote ) {
$request = new WP_REST_Request( 'GET', '/wp/v2/pattern-directory/patterns' );
$core_keyword_id = 11; // 11 is the ID for "core".
$request->set_param( 'keyword', $core_keyword_id );
$response = rest_do_request( $request );
if ( $response->is_error() ) {
return;
}
$patterns = $response->get_data();
foreach ( $patterns as $pattern ) {
$pattern['source'] = 'pattern-directory/core';
$normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
$pattern_name = 'core/' . sanitize_title( $normalized_pattern['title'] );
register_block_pattern( $pattern_name, $normalized_pattern );
}
}
}
Hooks
- apply_filters( ‘should_load_remote_block_patterns’, bool $should_load_remote )
-
Filter to disable remote block patterns.