Using WordPress ‘customize_loaded_components’ PHP filter

The customize_loaded_components WordPress PHP filter allows you to modify the core Customizer components that will be loaded. This enables you to exclude certain core components from being instantiated by filtering them out of the array.

Usage

add_filter('customize_loaded_components', 'my_custom_components', 10, 2);

function my_custom_components($components, $manager) {
    // your custom code here
    return $components;
}

Parameters

  • $components (string[]): An array of core components to load.
  • $manager (WP_Customize_Manager): The WP_Customize_Manager instance.

More information

See WordPress Developer Resources: customize_loaded_components

Please note that this filter usually runs during the plugins_loaded action, so it cannot be added in a theme.

Examples

Remove the ‘Widgets’ component from the Customizer

function remove_widgets_component($components, $manager) {
    $key = array_search('widgets', $components);
    if (false !== $key) {
        unset($components[$key]);
    }
    return $components;
}
add_filter('customize_loaded_components', 'remove_widgets_component', 10, 2);

Remove the ‘Menus’ component from the Customizer

function remove_menus_component($components, $manager) {
    $key = array_search('menus', $components);
    if (false !== $key) {
        unset($components[$key]);
    }
    return $components;
}
add_filter('customize_loaded_components', 'remove_menus_component', 10, 2);

Remove multiple components from the Customizer

function remove_multiple_components($components, $manager) {
    $components_to_remove = array('widgets', 'menus');
    $components = array_diff($components, $components_to_remove);
    return $components;
}
add_filter('customize_loaded_components', 'remove_multiple_components', 10, 2);

Conditionally remove a component based on user role

function remove_component_for_non_admins($components, $manager) {
    if (!current_user_can('manage_options')) {
        $key = array_search('widgets', $components);
        if (false !== $key) {
            unset($components[$key]);
        }
    }
    return $components;
}
add_filter('customize_loaded_components', 'remove_component_for_non_admins', 10, 2);

Add a custom component to the Customizer

function add_custom_component($components, $manager) {
    $components[] = 'my_custom_component';
    return $components;
}
add_filter('customize_loaded_components', 'add_custom_component', 10, 2);