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 一样,新数据需要声明其使用的 version(theme.json 版本),以便在版本不同时能够正确迁移到运行时版本。
示例:
以下是为主题传递新的调色板并禁用文本颜色 UI 的方法:
function wpdocs_filter_theme_json_theme( $theme_json ){
$new_data = array(
'version' => 2,
'settings' => array(
'color' => array(
'text' => false,
'palette' => array( /* New palette */
array(
'slug' => 'foreground',
'color' => 'black',
'name' => __( 'Foreground', 'theme-domain' ),
),
array(
'slug' => 'background',
'color' => 'white',
'name' => __( 'Background', 'theme-domain' ),
),
),
),
),
);
return $theme_json->update_with( $new_data );
}
add_filter( 'wp_theme_json_data_theme', 'wpdocs_filter_theme_json_theme' );