返回介绍

validate_current_theme()

发布于 2017-09-11 11:14:48 字数 3323 浏览 1032 评论 0 收藏 0

validate_current_theme()

Checks that current theme files ‘index.php’ and ‘style.css’ exists.


description

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 current theme does not validate.

You can use the ‘validate_current_theme’ filter to return false to disable this functionality.


返回值

(bool)


源代码

File: wp-includes/theme.php

function validate_current_theme() {
	/**
	 * Filters whether to validate the current theme.
	 *
	 * @since 2.7.0
	 *
	 * @param bool $validate Whether to validate the current theme. Default true.
	 */
	if ( wp_installing() || ! apply_filters( 'validate_current_theme', true ) )
		return true;

	if ( ! 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;
}

更新日志

Versiondescription
1.5.0Introduced.

相关函数

Uses

  • wp-includes/class-wp-theme.php: WP_Theme::get_core_default_theme()
  • wp-includes/load.php: wp_installing()
  • wp-includes/theme.php: switch_theme()
  • wp-includes/theme.php: validate_current_theme
  • wp-includes/theme.php: get_template_directory()
  • wp-includes/theme.php: get_stylesheet_directory()
  • wp-includes/theme.php: is_child_theme()
  • wp-includes/theme.php: wp_get_theme()
  • wp-includes/theme.php: get_stylesheet()
  • wp-includes/plugin.php: apply_filters()
  • Show 5 more uses Hide more uses

Used By

  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::after_setup_theme()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文