validate_current_theme()
云策文档标注
概述
validate_current_theme() 函数用于验证当前活动主题是否包含必需的文件,确保主题结构完整。如果验证失败,系统会回退到默认主题。
关键要点
- 验证独立主题需要 templates/index.html、block-templates/index.html(自 5.9.0 起已弃用)或 index.php 模板文件,以及 style.css 文件。
- 验证子主题需要父主题的 style.css 文件和子主题的 style.css 文件。
- 如果活动主题验证失败,会尝试切换到 WP_DEFAULT_THEME;若该主题不存在,则回退到最新的核心默认主题。
- 可通过 'validate_current_theme' 过滤器返回 false 来禁用此功能。
- 在 WordPress 安装模式(wp_installing())下,验证会被跳过。
代码示例
function validate_current_theme() {
if ( wp_installing() || ! apply_filters( 'validate_current_theme', true ) ) {
return true;
}
if (
! file_exists( get_template_directory() . '/templates/index.html' )
&& ! file_exists( get_template_directory() . '/block-templates/index.html' )
&& ! file_exists( get_template_directory() . '/index.php' )
) {
// Invalid.
} elseif ( ! file_exists( get_template_directory() . '/style.css' ) ) {
// Invalid.
} elseif ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
// Invalid.
} else {
return true;
}
$default = wp_get_theme( WP_DEFAULT_THEME );
if ( $default->exists() ) {
switch_theme( WP_DEFAULT_THEME );
return false;
}
$default = WP_Theme::get_core_default_theme();
if ( false === $default || get_stylesheet() === $default->get_stylesheet() ) {
return true;
}
switch_theme( $default->get_stylesheet() );
return false;
}注意事项
- 自 WordPress 6.0.0 起,块主题不再需要 index.php 模板文件。
- 函数最初不会检查默认主题,但若活动主题无效且默认主题不存在,会回退到最新的核心默认主题。
- 避免在验证失败时陷入无限循环,函数包含针对 WP_DEFAULT_THEME 和当前主题的检查。
原文内容
Checks that the active theme has the required files.
Description
Standalone themes need to have a templates/index.html or index.php template file.
Child themes need to have a Template header in the style.css stylesheet.
Does not initially check the default theme, which is the fallback and should always exist.
But if it doesn’t exist, it’ll fall back to the latest core default theme that does exist.
Will switch theme to the fallback theme if active theme does not validate.
You can use the ‘validate_current_theme’ filter to return false to disable this functionality.
See also
Source
function validate_current_theme() {
/**
* Filters whether to validate the active theme.
*
* @since 2.7.0
*
* @param bool $validate Whether to validate the active theme. Default true.
*/
if ( wp_installing() || ! apply_filters( 'validate_current_theme', true ) ) {
return true;
}
if (
! file_exists( get_template_directory() . '/templates/index.html' )
&& ! file_exists( get_template_directory() . '/block-templates/index.html' ) // Deprecated path support since 5.9.0.
&& ! file_exists( get_template_directory() . '/index.php' )
) {
// Invalid.
} elseif ( ! file_exists( get_template_directory() . '/style.css' ) ) {
// Invalid.
} elseif ( is_child_theme() && ! file_exists( get_stylesheet_directory() . '/style.css' ) ) {
// Invalid.
} else {
// Valid.
return true;
}
$default = wp_get_theme( WP_DEFAULT_THEME );
if ( $default->exists() ) {
switch_theme( WP_DEFAULT_THEME );
return false;
}
/**
* If we're in an invalid state but WP_DEFAULT_THEME doesn't exist,
* switch to the latest core default theme that's installed.
*
* If it turns out that this latest core default theme is our current
* theme, then there's nothing we can do about that, so we have to bail,
* rather than going into an infinite loop. (This is why there are
* checks against WP_DEFAULT_THEME above, also.) We also can't do anything
* if it turns out there is no default theme installed. (That's `false`.)
*/
$default = WP_Theme::get_core_default_theme();
if ( false === $default || get_stylesheet() === $default->get_stylesheet() ) {
return true;
}
switch_theme( $default->get_stylesheet() );
return false;
}
Hooks
- apply_filters( ‘validate_current_theme’, bool $validate )
-
Filters whether to validate the active theme.