WordPress.org

Make WordPress Core

Changes between Initial Version and Version 1 of Ticket #33552, comment 6


Ignore:
Timestamp:
10/20/2015 08:10:47 PM (4 years ago)
Author:
westonruter
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #33552, comment 6

    initial v1  
    33To me it seems the primary use case of this filter is to remove components from being added. For instance:
    44
    5 {{{
     5{{{#!php
     6<?php
    67add_filter( 'customize_components_to_load', function( $components ) {
    78    return array_filter( $components, function ( $component ) {
     
    1516If I want to introduce a new component for “posts” then it seems I would need to do basically hack the filter to serve as an action:
    1617
    17 {{{
     18{{{#!php
     19<?php
    1820add_filter( 'customize_components_to_load', function( $components, $wp_customize ) {
    1921    $wp_customize->posts = new My_Customize_Posts( $wp_customize );
     
    2426If I want to replace the current `widgets` implementation with a different one, I would have to do something like:
    2527
    26 {{{
     28{{{#!php
     29<?php
    2730add_filter( 'customize_components_to_load', function( $components, $wp_customize ) {
    2831    // First remove widgets entirely so that it won't get constructed.
     
    4144Maybe it would work better if `customize_components_to_load` was filtering a mapping of component name to class name. For instance:
    4245
    43 {{{
     46{{{#!php
     47<?php
    4448$components = array(
    4549    'widgets' => 'WP_Customize_Widgets',
     
    5155And then it could do:
    5256
    53 {{{
     57{{{#!php
     58<?php
    5459foreach ( $components as $component => $class ) {
    5560    $this->$component = new $class( $this );
     
    5964This would, however, not play nicely with the `require`ing of the class file, so those should `require_once` statements should remain where they are at the top, or else changed to be something lower down like:
    6065
    61 {{{
     66{{{#!php
     67<?php
    6268if ( isset( $components['widgets'] ) ) {
    6369    require_once( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
     
    7076What this implies is that `\WP_Customize_Widgets` and `\WP_Customize_Nav_Menus` can never be pluggable to be replaced with any other classes. If you want to use a different class for managing menus, then you'd have to do:
    7177
    72 {{{
     78{{{#!php
     79<?php
    7380add_filter( 'customize_components_to_load', function( $components) {
    7481    require_once MY_PLUGIN_DIR . '/class-my-customize-nav-menus.php';