函数文档

_remove_theme_support()

💡 云策文档标注

概述

_remove_theme_support() 是一个内部函数,用于移除主题支持的功能,但开发者不应直接使用,因为它可能影响未直接使用的内部特性。

关键要点

  • 函数 _remove_theme_support() 用于移除主题支持,但文档明确警告开发者不要使用。
  • 参数 $feature 指定要移除的功能,支持的值列表可参考 add_theme_support(),包括 'custom-logo'、'post-thumbnails' 等。
  • 函数返回布尔值:如果支持被移除则返回 true,如果功能未注册则返回 false。
  • 内部实现涉及全局变量 $_wp_theme_features 和特定功能(如 'custom-header'、'custom-background')的清理操作。

代码示例

function _remove_theme_support( $feature ) {
    global $_wp_theme_features;

    switch ( $feature ) {
        case 'custom-header-uploads':
            if ( ! isset( $_wp_theme_features['custom-header'] ) ) {
                return false;
            }
            add_theme_support( 'custom-header', array( 'uploads' => false ) );
            return; // Do not continue - custom-header-uploads no longer exists.
    }

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

    switch ( $feature ) {
        case 'custom-header':
            if ( ! did_action( 'wp_loaded' ) ) {
                break;
            }
            $support = get_theme_support( 'custom-header' );
            if ( isset( $support[0]['wp-head-callback'] ) ) {
                remove_action( 'wp_head', $support[0]['wp-head-callback'] );
            }
            if ( isset( $GLOBALS['custom_image_header'] ) ) {
                remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
                unset( $GLOBALS['custom_image_header'] );
            }
            break;

        case 'custom-background':
            if ( ! did_action( 'wp_loaded' ) ) {
                break;
            }
            $support = get_theme_support( 'custom-background' );
            if ( isset( $support[0]['wp-head-callback'] ) ) {
                remove_action( 'wp_head', $support[0]['wp-head-callback'] );
            }
            remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
            unset( $GLOBALS['custom_background'] );
            break;
    }

    unset( $_wp_theme_features[ $feature ] );

    return true;
}

注意事项

  • 此函数是内部函数,开发者应避免直接调用,以免影响主题或插件的正常功能。
  • 移除主题支持时,需确保功能已通过 add_theme_support() 注册,否则函数将返回 false。
  • 对于 'custom-header' 和 'custom-background' 等特定功能,函数会执行额外的清理操作,如移除相关的 action hooks。

📄 原文内容

Do not use. Removes theme support internally without knowledge of those not used by themes directly.

Parameters

$featurestringrequired
The feature being removed. 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'

Return

bool True if support was removed, false if the feature was not registered.

Source

function _remove_theme_support( $feature ) {
	global $_wp_theme_features;

	switch ( $feature ) {
		case 'custom-header-uploads':
			if ( ! isset( $_wp_theme_features['custom-header'] ) ) {
				return false;
			}
			add_theme_support( 'custom-header', array( 'uploads' => false ) );
			return; // Do not continue - custom-header-uploads no longer exists.
	}

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

	switch ( $feature ) {
		case 'custom-header':
			if ( ! did_action( 'wp_loaded' ) ) {
				break;
			}
			$support = get_theme_support( 'custom-header' );
			if ( isset( $support[0]['wp-head-callback'] ) ) {
				remove_action( 'wp_head', $support[0]['wp-head-callback'] );
			}
			if ( isset( $GLOBALS['custom_image_header'] ) ) {
				remove_action( 'admin_menu', array( $GLOBALS['custom_image_header'], 'init' ) );
				unset( $GLOBALS['custom_image_header'] );
			}
			break;

		case 'custom-background':
			if ( ! did_action( 'wp_loaded' ) ) {
				break;
			}
			$support = get_theme_support( 'custom-background' );
			if ( isset( $support[0]['wp-head-callback'] ) ) {
				remove_action( 'wp_head', $support[0]['wp-head-callback'] );
			}
			remove_action( 'admin_menu', array( $GLOBALS['custom_background'], 'init' ) );
			unset( $GLOBALS['custom_background'] );
			break;
	}

	unset( $_wp_theme_features[ $feature ] );

	return true;
}

Changelog

Version Description
3.1.0 Introduced.