钩子文档

customize_loaded_components

💡 云策文档标注

概述

customize_loaded_components 是一个 WordPress 过滤器,用于控制核心 Customizer 组件的加载。它允许开发者通过过滤数组来排除特定组件,从而定制 Customizer 的界面和功能。

关键要点

  • 过滤器名称:customize_loaded_components
  • 作用:过滤核心 Customizer 组件数组,以排除或修改要加载的组件
  • 参数:$components(字符串数组,核心组件列表)和 $manager(WP_Customize_Manager 实例)
  • 运行时机:通常在 'plugins_loaded' 动作期间运行,因此不能在主题中添加此过滤器
  • 相关函数:WP_Customize_Manager::__construct()
  • 引入版本:4.4.0

代码示例

/**
 * Removes the core 'Menus' panel from the Customizer.
 *
 * @param array $components Core Customizer components list.
 * @return array (Maybe) modified components list.
 */
function wpdocs_remove_nav_menus_panel( $components ) {
	$i = array_search( 'nav_menus', $components );
	if ( false !== $i ) {
		unset( $components[ $i ] );
	}
	return $components;
}
add_filter( 'customize_loaded_components', 'wpdocs_remove_nav_menus_panel' );
/**
 * Removes the core 'Widgets' panel from the Customizer.
 *
 * @param array $components Core Customizer components list.
 * @return array (Maybe) modified components list.
 */
function wpdocs_remove_widgets_panel( $components ) {
	$i = array_search( 'widgets', $components );
	if ( false !== $i ) {
		unset( $components[ $i ] );
	}
	return $components;
}
add_filter( 'customize_loaded_components', 'wpdocs_remove_widgets_panel' );

📄 原文内容

Filters the core Customizer components to load.

Description

This allows Core components to be excluded from being instantiated by filtering them out of the array. Note that this filter generally runs during the ‘plugins_loaded’ action, so it cannot be added in a theme.

See also

Parameters

$componentsstring[]
Array of core components to load.
$managerWP_Customize_Manager

Source

$components = apply_filters( 'customize_loaded_components', $this->components, $this );

Changelog

Version Description
4.4.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Remove the ‘Menus’ panel from the Customizer

    /**
     * Removes the core 'Menus' panel from the Customizer.
     *
     * @param array $components Core Customizer components list.
     * @return array (Maybe) modified components list.
     */
    function wpdocs_remove_nav_menus_panel( $components ) {
    	$i = array_search( 'nav_menus', $components );
    	if ( false !== $i ) {
    		unset( $components[ $i ] );
    	}
    	return $components;
    }
    add_filter( 'customize_loaded_components', 'wpdocs_remove_nav_menus_panel' );

  2. Skip to note 4 content

    Remove the ‘Widgets’ panel from the Customizer

    /**
     * Removes the core 'Widgets' panel from the Customizer.
     *
     * @param array $components Core Customizer components list.
     * @return array (Maybe) modified components list.
     */
    function wpdocs_remove_widgets_panel( $components ) {
    	$i = array_search( 'widgets', $components );
    	if ( false !== $i ) {
    		unset( $components[ $i ] );
    	}
    	return $components;
    }
    add_filter( 'customize_loaded_components', 'wpdocs_remove_widgets_panel' );