get_editor_stylesheets()
云策文档标注
概述
get_editor_stylesheets() 函数用于检索已注册的编辑器样式表 URL 列表。它通过全局变量 $editor_styles 获取配置,并处理外部引用、父主题和子主题的样式表加载顺序。
关键要点
- 函数返回一个字符串数组,包含已注册的编辑器样式表 URL。
- 支持外部样式表(如字体)的引用,使用 sanitize_url() 进行清理。
- 在子主题中,优先从父主题加载样式表,以确保子主题 CSS 可以覆盖。
- 使用 apply_filters('editor_stylesheets', $stylesheets) 钩子允许过滤样式表数组。
代码示例
function get_editor_stylesheets() {
$stylesheets = array();
if ( ! empty( $GLOBALS['editor_styles'] ) && is_array( $GLOBALS['editor_styles'] ) ) {
$editor_styles = $GLOBALS['editor_styles'];
$editor_styles = array_unique( array_filter( $editor_styles ) );
$style_uri = get_stylesheet_directory_uri();
$style_dir = get_stylesheet_directory();
foreach ( $editor_styles as $key => $file ) {
if ( preg_match( '~^(https?:)?//~', $file ) ) {
$stylesheets[] = sanitize_url( $file );
unset( $editor_styles[ $key ] );
}
}
if ( is_child_theme() ) {
$template_uri = get_template_directory_uri();
$template_dir = get_template_directory();
foreach ( $editor_styles as $key => $file ) {
if ( $file && file_exists( "$template_dir/$file" ) ) {
$stylesheets[] = "$template_uri/$file";
}
}
}
foreach ( $editor_styles as $file ) {
if ( $file && file_exists( "$style_dir/$file" ) ) {
$stylesheets[] = "$style_uri/$file";
}
}
}
return apply_filters( 'editor_stylesheets', $stylesheets );
}注意事项
- 函数依赖于全局变量 $editor_styles 来获取样式表配置,需确保主题已正确注册。
- 在子主题环境中,样式表加载顺序为父主题优先,这有助于实现 CSS 覆盖逻辑。
- 使用 editor_stylesheets 过滤器可以动态修改返回的样式表 URL 数组。
原文内容
Retrieves any registered editor stylesheet URLs.
Source
function get_editor_stylesheets() {
$stylesheets = array();
// Load editor_style.css if the active theme supports it.
if ( ! empty( $GLOBALS['editor_styles'] ) && is_array( $GLOBALS['editor_styles'] ) ) {
$editor_styles = $GLOBALS['editor_styles'];
$editor_styles = array_unique( array_filter( $editor_styles ) );
$style_uri = get_stylesheet_directory_uri();
$style_dir = get_stylesheet_directory();
// Support externally referenced styles (like, say, fonts).
foreach ( $editor_styles as $key => $file ) {
if ( preg_match( '~^(https?:)?//~', $file ) ) {
$stylesheets[] = sanitize_url( $file );
unset( $editor_styles[ $key ] );
}
}
// Look in a parent theme first, that way child theme CSS overrides.
if ( is_child_theme() ) {
$template_uri = get_template_directory_uri();
$template_dir = get_template_directory();
foreach ( $editor_styles as $key => $file ) {
if ( $file && file_exists( "$template_dir/$file" ) ) {
$stylesheets[] = "$template_uri/$file";
}
}
}
foreach ( $editor_styles as $file ) {
if ( $file && file_exists( "$style_dir/$file" ) ) {
$stylesheets[] = "$style_uri/$file";
}
}
}
/**
* Filters the array of URLs of stylesheets applied to the editor.
*
* @since 4.3.0
*
* @param string[] $stylesheets Array of URLs of stylesheets to be applied to the editor.
*/
return apply_filters( 'editor_stylesheets', $stylesheets );
}
Hooks
- apply_filters( ‘editor_stylesheets’, string[] $stylesheets )
-
Filters the array of URLs of stylesheets applied to the editor.
Changelog
| Version | Description |
|---|---|
| 4.0.0 | Introduced. |