42 lines
1.5 KiB
Markdown
42 lines
1.5 KiB
Markdown
|
|
# 全局样式过滤器
|
|||
|
|
|
|||
|
|
WordPress 6.1 引入了一些服务端过滤器,用于挂接到不同数据层提供的 `theme.json` 数据:
|
|||
|
|
|
|||
|
|
- `wp_theme_json_data_default`:挂接到 WordPress 提供的默认数据
|
|||
|
|
- `wp_theme_json_data_blocks`:挂接到区块提供的数据
|
|||
|
|
- `wp_theme_json_data_theme`:挂接到主题提供的数据
|
|||
|
|
- `wp_theme_json_data_user`:挂接到用户提供的数据
|
|||
|
|
|
|||
|
|
每个过滤器都会接收到一个包含对应层级数据的 `WP_Theme_JSON_Data` 类实例。要提供新数据,过滤器回调函数需要使用 `update_with( $new_data )` 方法,其中 `$new_data` 是有效的类 `theme.json` 结构。与任何 `theme.json` 一样,新数据需要声明所使用的 `theme.json` 的 `version`,以便在版本不同时能正确迁移到运行时版本。
|
|||
|
|
|
|||
|
|
_示例:_
|
|||
|
|
|
|||
|
|
以下是如何为主题传递新调色板并禁用文本颜色用户界面的方法:
|
|||
|
|
|
|||
|
|
```php
|
|||
|
|
function wpdocs_filter_theme_json_theme( $theme_json ){
|
|||
|
|
$new_data = array(
|
|||
|
|
'version' => 2,
|
|||
|
|
'settings' => array(
|
|||
|
|
'color' => array(
|
|||
|
|
'text' => false,
|
|||
|
|
'palette' => array( /* 新调色板 */
|
|||
|
|
array(
|
|||
|
|
'slug' => 'foreground',
|
|||
|
|
'color' => 'black',
|
|||
|
|
'name' => __( '前景色', 'theme-domain' ),
|
|||
|
|
),
|
|||
|
|
array(
|
|||
|
|
'slug' => 'background',
|
|||
|
|
'color' => 'white',
|
|||
|
|
'name' => __( '背景色', 'theme-domain' ),
|
|||
|
|
),
|
|||
|
|
),
|
|||
|
|
),
|
|||
|
|
),
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
return $theme_json->update_with( $new_data );
|
|||
|
|
}
|
|||
|
|
add_filter( 'wp_theme_json_data_theme', 'wpdocs_filter_theme_json_theme' );
|
|||
|
|
```
|