函数文档

_custom_header_background_just_in_time()

💡 云策文档标注

概述

_custom_header_background_just_in_time() 函数用于在 WordPress 中即时注册自定义页眉和背景的内部处理逻辑。它检查主题是否支持相关功能,并动态添加必要的动作和类实例。

关键要点

  • 函数检查主题是否支持 'custom-header' 和 'custom-background',使用 current_theme_supports() 进行验证。
  • 对于自定义页眉,如果支持,则重新调用 add_theme_support() 并添加 wp_head 回调;在管理界面中实例化 Custom_Image_Header 类。
  • 对于自定义背景,类似地处理,添加 wp_head 回调并在管理界面中实例化 Custom_Background 类。
  • 函数依赖于全局变量 $custom_image_header 和 $custom_background 来存储实例。

代码示例

function _custom_header_background_just_in_time() {
	global $custom_image_header, $custom_background;

	if ( current_theme_supports( 'custom-header' ) ) {
		add_theme_support( 'custom-header', array( '__jit' => true ) );
		$args = get_theme_support( 'custom-header' );
		if ( $args[0]['wp-head-callback'] ) {
			add_action( 'wp_head', $args[0]['wp-head-callback'] );
		}
		if ( is_admin() ) {
			require_once ABSPATH . 'wp-admin/includes/class-custom-image-header.php';
			$custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
		}
	}

	if ( current_theme_supports( 'custom-background' ) ) {
		add_theme_support( 'custom-background', array( '__jit' => true ) );
		$args = get_theme_support( 'custom-background' );
		add_action( 'wp_head', $args[0]['wp-head-callback'] );
		if ( is_admin() ) {
			require_once ABSPATH . 'wp-admin/includes/class-custom-background.php';
			$custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
		}
	}
}

注意事项

  • 此函数是内部函数,通常由 WordPress 核心自动调用,开发者无需直接使用。
  • 确保主题正确声明对 'custom-header' 和 'custom-background' 的支持,否则函数不会执行相关操作。
  • 在管理界面中,类实例化依赖于 admin-head-callback 和 admin-preview-callback 参数。

📄 原文内容

Registers the internal custom header and background routines.

Source

function _custom_header_background_just_in_time() {
	global $custom_image_header, $custom_background;

	if ( current_theme_supports( 'custom-header' ) ) {
		// In case any constants were defined after an add_custom_image_header() call, re-run.
		add_theme_support( 'custom-header', array( '__jit' => true ) );

		$args = get_theme_support( 'custom-header' );
		if ( $args[0]['wp-head-callback'] ) {
			add_action( 'wp_head', $args[0]['wp-head-callback'] );
		}

		if ( is_admin() ) {
			require_once ABSPATH . 'wp-admin/includes/class-custom-image-header.php';
			$custom_image_header = new Custom_Image_Header( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
		}
	}

	if ( current_theme_supports( 'custom-background' ) ) {
		// In case any constants were defined after an add_custom_background() call, re-run.
		add_theme_support( 'custom-background', array( '__jit' => true ) );

		$args = get_theme_support( 'custom-background' );
		add_action( 'wp_head', $args[0]['wp-head-callback'] );

		if ( is_admin() ) {
			require_once ABSPATH . 'wp-admin/includes/class-custom-background.php';
			$custom_background = new Custom_Background( $args[0]['admin-head-callback'], $args[0]['admin-preview-callback'] );
		}
	}
}

Changelog

Version Description
3.4.0 Introduced.