函数文档

current_theme_supports()

💡 云策文档标注

概述

current_theme_supports() 函数用于检查当前激活主题是否支持特定功能。它基于 add_theme_support() 注册的功能列表进行验证,并返回布尔值。

关键要点

  • 函数接受一个必需参数 $feature(字符串),指定要检查的功能,如 'custom-logo' 或 'html5'。
  • 可选参数 $args(混合类型)用于针对某些功能进行额外检查,例如 'post-thumbnails' 的内容类型。
  • 返回值为布尔值:true 表示主题支持该功能,false 表示不支持。
  • 函数内部处理了特定功能(如 'custom-header-uploads'、'post-thumbnails'、'html5'、'post-formats'、'custom-logo'、'custom-header'、'custom-background')的特殊逻辑。
  • 通过 apply_filters( "current_theme_supports-{$feature}" ) 钩子允许动态过滤支持状态。

代码示例

current_theme_supports( 'custom-logo' );
current_theme_supports( 'html5', 'comment-form' );

注意事项

  • 功能列表参考 add_theme_support(),包括 'admin-bar'、'align-wide'、'custom-logo' 等核心值。
  • 在 WordPress 5.3.0 中正式引入了 ...$args 参数,2.9.0 版本首次引入此函数。
  • 使用时需确保主题已通过 add_theme_support() 正确注册功能,否则可能返回 false。

📄 原文内容

Checks a theme’s support for a given feature.

Description

Example usage:

current_theme_supports( 'custom-logo' );
current_theme_supports( 'html5', 'comment-form' );

Parameters

$featurestringrequired
The feature being checked. See add_theme_support() for the list of possible values.

More Arguments from add_theme_support( … $feature )

The feature being added. Likely core values include:

  • 'admin-bar'
  • 'align-wide'
  • 'appearance-tools'
  • 'automatic-feed-links'
  • 'block-templates'
  • 'block-template-parts'
  • 'border'
  • 'core-block-patterns'
  • 'custom-background'
  • 'custom-header'
  • 'custom-line-height'
  • 'custom-logo'
  • 'customize-selective-refresh-widgets'
  • 'custom-spacing'
  • 'custom-units'
  • 'dark-editor-style'
  • 'disable-custom-colors'
  • 'disable-custom-font-sizes'
  • 'disable-custom-gradients'
  • 'disable-layout-styles'
  • 'editor-color-palette'
  • 'editor-gradient-presets'
  • 'editor-font-sizes'
  • 'editor-spacing-sizes'
  • 'editor-styles'
  • 'featured-content'
  • 'html5'
  • 'link-color'
  • 'menus'
  • 'post-formats'
  • 'post-thumbnails'
  • 'responsive-embeds'
  • 'starter-content'
  • 'title-tag'
  • 'widgets'
  • 'widgets-block-editor'
  • 'wp-block-styles'

$argsmixedoptional
Optional extra arguments to be checked against certain features.

Return

bool True if the active theme supports the feature, false otherwise.

Source

function current_theme_supports( $feature, ...$args ) {
	global $_wp_theme_features;

	if ( 'custom-header-uploads' === $feature ) {
		return current_theme_supports( 'custom-header', 'uploads' );
	}

	if ( ! isset( $_wp_theme_features[ $feature ] ) ) {
		return false;
	}

	// If no args passed then no extra checks need to be performed.
	if ( ! $args ) {
		/** This filter is documented in wp-includes/theme.php */
		return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
	}

	switch ( $feature ) {
		case 'post-thumbnails':
			/*
			 * post-thumbnails can be registered for only certain content/post types
			 * by passing an array of types to add_theme_support().
			 * If no array was passed, then any type is accepted.
			 */
			if ( true === $_wp_theme_features[ $feature ] ) {  // Registered for all types.
				return true;
			}
			$content_type = $args[0];
			return in_array( $content_type, $_wp_theme_features[ $feature ][0], true );

		case 'html5':
		case 'post-formats':
			/*
			 * Specific post formats can be registered by passing an array of types
			 * to add_theme_support().
			 *
			 * Specific areas of HTML5 support *must* be passed via an array to add_theme_support().
			 */
			$type = $args[0];
			return in_array( $type, $_wp_theme_features[ $feature ][0], true );

		case 'custom-logo':
		case 'custom-header':
		case 'custom-background':
			// Specific capabilities can be registered by passing an array to add_theme_support().
			return ( isset( $_wp_theme_features[ $feature ][0][ $args[0] ] ) && $_wp_theme_features[ $feature ][0][ $args[0] ] );
	}

	/**
	 * Filters whether the active theme supports a specific feature.
	 *
	 * The dynamic portion of the hook name, `$feature`, refers to the specific
	 * theme feature. See add_theme_support() for the list of possible values.
	 *
	 * @since 3.4.0
	 *
	 * @param bool   $supports Whether the active theme supports the given feature. Default true.
	 * @param array  $args     Array of arguments for the feature.
	 * @param string $feature  The theme feature.
	 */
	return apply_filters( "current_theme_supports-{$feature}", true, $args, $_wp_theme_features[ $feature ] ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}

Hooks

apply_filters( “current_theme_supports-{$feature}”, bool $supports, array $args, string $feature )

Filters whether the active theme supports a specific feature.

Changelog

Version Description
5.3.0 Formalized the existing and already documented ...$args parameter by adding it to the function signature.
2.9.0 Introduced.

User Contributed Notes