validate_theme_requirements

函数
validate_theme_requirements ( $stylesheet )
参数
  • (string) $stylesheet Directory name for the theme.
    Required:
返回值
  • (true|WP_Error) True if requirements are met, WP_Error on failure.
定义位置
相关方法
validate_plugin_requirementsvalidate_active_pluginswp_validate_redirectwp_validate_user_request_keyvalidate_email
引入
5.5.0
弃用
-

validate_theme_requirements。这个WordPress函数用于在安装一个主题之前验证它的要求。它检查主题的最低WordPress版本、PHP版本和其他要求是否满足,如果发现任何错误则返回错误信息。

验证主题对WordPress版本和PHP版本的要求。

使用主题的`style.css`文件中定义的`Requires at least`和`Requires PHP`标题信息。

function validate_theme_requirements( $stylesheet ) {
	$theme = wp_get_theme( $stylesheet );

	$requirements = array(
		'requires'     => ! empty( $theme->get( 'RequiresWP' ) ) ? $theme->get( 'RequiresWP' ) : '',
		'requires_php' => ! empty( $theme->get( 'RequiresPHP' ) ) ? $theme->get( 'RequiresPHP' ) : '',
	);

	$compatible_wp  = is_wp_version_compatible( $requirements['requires'] );
	$compatible_php = is_php_version_compatible( $requirements['requires_php'] );

	if ( ! $compatible_wp && ! $compatible_php ) {
		return new WP_Error(
			'theme_wp_php_incompatible',
			sprintf(
				/* translators: %s: Theme name. */
				_x( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.', 'theme' ),
				$theme->display( 'Name' )
			)
		);
	} elseif ( ! $compatible_php ) {
		return new WP_Error(
			'theme_php_incompatible',
			sprintf(
				/* translators: %s: Theme name. */
				_x( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.', 'theme' ),
				$theme->display( 'Name' )
			)
		);
	} elseif ( ! $compatible_wp ) {
		return new WP_Error(
			'theme_wp_incompatible',
			sprintf(
				/* translators: %s: Theme name. */
				_x( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.', 'theme' ),
				$theme->display( 'Name' )
			)
		);
	}

	return true;
}

常见问题

FAQs
查看更多 >