类文档

WP_Customize_Background_Image_Control

💡 云策文档标注

概述

WP_Customize_Background_Image_Control 是 WordPress 自定义器中的一个控件类,用于管理背景图像设置。它继承自 WP_Customize_Image_Control,提供特定于背景图像的功能。

关键要点

  • 继承自 WP_Customize_Image_Control,类型为 'background'。
  • 构造函数设置控件标签为 'Background Image',并关联到 'background_image' 部分。
  • enqueue 方法用于加载相关脚本和样式,包括本地化数据以支持自定义背景功能。

代码示例

public function __construct( $manager ) {
    parent::__construct(
        $manager,
        'background_image',
        array(
            'label'   => __( 'Background Image' ),
            'section' => 'background_image',
        )
    );
}
public function enqueue() {
    parent::enqueue();

    $custom_background = get_theme_support( 'custom-background' );
    wp_localize_script(
        'customize-controls',
        '_wpCustomizeBackground',
        array(
            'defaults' => ! empty( $custom_background[0] ) ? $custom_background[0] : array(),
            'nonces'   => array(
                'add' => wp_create_nonce( 'background-add' ),
            ),
        )
    );
}

注意事项

自 WordPress 3.4.0 版本引入,需确保主题支持 'custom-background' 以充分利用此控件功能。


📄 原文内容

Customize Background Image Control class.

Description

See also

Methods

Name Description
WP_Customize_Background_Image_Control::__construct Constructor.
WP_Customize_Background_Image_Control::enqueue Enqueue control related scripts/styles.

Source

class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {

	/**
	 * Customize control type.
	 *
	 * @since 4.1.0
	 * @var string
	 */
	public $type = 'background';

	/**
	 * Constructor.
	 *
	 * @since 3.4.0
	 * @uses WP_Customize_Image_Control::__construct()
	 *
	 * @param WP_Customize_Manager $manager Customizer bootstrap instance.
	 */
	public function __construct( $manager ) {
		parent::__construct(
			$manager,
			'background_image',
			array(
				'label'   => __( 'Background Image' ),
				'section' => 'background_image',
			)
		);
	}

	/**
	 * Enqueue control related scripts/styles.
	 *
	 * @since 4.1.0
	 */
	public function enqueue() {
		parent::enqueue();

		$custom_background = get_theme_support( 'custom-background' );
		wp_localize_script(
			'customize-controls',
			'_wpCustomizeBackground',
			array(
				'defaults' => ! empty( $custom_background[0] ) ? $custom_background[0] : array(),
				'nonces'   => array(
					'add' => wp_create_nonce( 'background-add' ),
				),
			)
		);
	}
}

Changelog

Version Description
3.4.0 Introduced.