
如何开发一款WordPress子主题
validate_current_theme ( No parameters )
validate_current_theme: 这个WordPress函数用来验证一个网站上当前活动的主题。它检查主题是否有效,是否与当前版本的WordPress兼容,如果发现任何错误,则返回一个错误信息数组。
检查活动主题是否拥有所需的文件。
独立主题需要有一个`templates/index.html`或`index.php`模板文件。子主题需要在`style.css`样式表中有一个`Template`标题。
最初不检查默认主题,默认主题是备用的,应该总是存在。但如果它不存在,它将返回到存在的最新核心默认主题。如果活动主题没有验证,就会将主题切换到后备主题。
你可以使用{@see ‘validate_current_theme’}过滤器来返回false以禁用这个功能。
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; }