wp_add_editor_classic_theme_styles()
云策文档标注
概述
wp_add_editor_classic_theme_styles() 函数用于在经典主题的编辑器中加载经典主题样式,主要用于向后兼容 Button 和 File 块。该函数已在 WordPress 6.8.0 版本中被弃用。
关键要点
- 函数 wp_add_editor_classic_theme_styles() 已弃用,建议使用 wp_enqueue_classic_theme_styles() 替代。
- 该函数检查主题是否使用 theme.json 文件,如果是则直接返回编辑器设置,不加载经典样式。
- 函数从 WordPress 核心目录加载经典主题 CSS 文件,并将其作为样式设置添加到编辑器设置数组的开头,以便主题可以覆盖。
- 相关函数包括 wp_theme_has_theme_json()、wp_scripts_get_suffix() 和 _deprecated_function()。
代码示例
function wp_add_editor_classic_theme_styles( $editor_settings ) {
_deprecated_function( __FUNCTION__, '6.8.0', 'wp_enqueue_classic_theme_styles' );
if ( wp_theme_has_theme_json() ) {
return $editor_settings;
}
$suffix = wp_scripts_get_suffix();
$classic_theme_styles = ABSPATH . WPINC . "/css/classic-themes$suffix.css";
$classic_theme_styles_settings = array(
'css' => file_get_contents( $classic_theme_styles ),
'__unstableType' => 'core',
'isGlobalStyles' => false,
);
array_unshift( $editor_settings['styles'], $classic_theme_styles_settings );
return $editor_settings;
}注意事项
- 此函数自 WordPress 6.8.0 起已弃用,开发者应更新代码以避免使用。
- 函数仅适用于经典主题(无 theme.json 文件),对于使用 theme.json 的主题,样式不会加载。
- 加载的样式文件路径基于 wp_scripts_get_suffix() 返回的后缀,确保使用正确的 CSS 文件版本。
原文内容
Loads classic theme styles on classic themes in the editor.
Description
This is used for backwards compatibility for Button and File blocks specifically.
Parameters
$editor_settingsarrayrequired-
The array of editor settings.
Source
function wp_add_editor_classic_theme_styles( $editor_settings ) {
_deprecated_function( __FUNCTION__, '6.8.0', 'wp_enqueue_classic_theme_styles' );
if ( wp_theme_has_theme_json() ) {
return $editor_settings;
}
$suffix = wp_scripts_get_suffix();
$classic_theme_styles = ABSPATH . WPINC . "/css/classic-themes$suffix.css";
/*
* This follows the pattern of get_block_editor_theme_styles,
* but we can't use get_block_editor_theme_styles directly as it
* only handles external files or theme files.
*/
$classic_theme_styles_settings = array(
'css' => file_get_contents( $classic_theme_styles ),
'__unstableType' => 'core',
'isGlobalStyles' => false,
);
// Add these settings to the start of the array so that themes can override them.
array_unshift( $editor_settings['styles'], $classic_theme_styles_settings );
return $editor_settings;
}