validate_current_theme

函式
validate_current_theme ( No parameters )
返回值
  • (bool)
相關
  • WP_DEFAULT_THEME
定義位置
相關方法
get_current_themevalidate_usernameget_current_site_namecurrent_theme_infocurrent_time
引入
1.5.0
棄用
-

validate_current_theme: 這個WordPress函式用來驗證一個網站上當前活動的主題。它檢查主題是否有效,是否與當前版本的WordPress相容,如果發現任何錯誤,則返回一個錯誤資訊陣列。

檢查活動主題是否擁有所需的檔案。

獨立主題需要有一個`templates/index.html`或`index.php`模板檔案。子主題需要在`style.css`樣式表中有一個`Template`標題。

最初不檢查預設主題,預設主題是備用的,應該總是存在。但如果它不存在,它將返回到存在的最新核心預設主題。如果活動主題沒有驗證,就會將主題切換到後備主題。

你可以使用{@see ‘validate_current_theme’}過濾器來返回false以禁用這個功能。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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;
}

常見問題

FAQs
檢視更多 >