1

I have written a WordPress theme which uses the customizer a lot. I would like to de-register the widgets panel (I have my reasons). Currently, I'm doing this:

/**
 * Our class for altering the WP theme customizer.
 */
class LXB_AF_Customize {

    public function __construct() {

        [...]

        add_action( 'customize_register', array( $this, 'remove_items' ), 980 );

        [...]

    }

    [...]

    // We're gonna output widgets elsewhere, so we don't need the core widgets panel.
    function remove_items( $wp_customize ) {

        [...] 

        @$wp_customize -> remove_panel( 'widgets' );

    }

}

Which is instantiated in the global scope via:

/**
 * Setup the Theme Customizer settings and controls.
 */
function lxb_af_customizer_init() {
    new LXB_AF_Customize();
}
add_action( 'init' , 'lxb_af_customizer_init' );

This triggers a doing_it_wrong error from core, here: https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-customize-manager.php#L1212

That message suggests that I instead use customize_loaded_components:

https://developer.wordpress.org/reference/hooks/customize_loaded_components/

However, as noted on that docs page, customize_loaded_components does not seem to work when invoked from a theme. I can only get it to work from a plugin. In my situation, I don't want to require any particular plugin to be active, in order to use my theme.

Any suggestions?

  • Just an idea, if you unregister_sidebar() any widget areas that have been created, will this take care of it? I know this hides the Widget menu option under Themes, just not sure about how it affects the Customizer. – Tim Malone May 2 '16 at 21:39
  • I want to use the sidebars eventually, I'm just using them in a different part of the customizer. (I'm nesting them into different panels for header/main/footer, because I have tons of sidebars and it's overwhelming to have them all top-level). – Scott Fennell May 4 '16 at 16:27
1

Since WordPress 4.5, there is a new warning regarding the use of remove_panel with core components (at the moment 'nav_menus' and 'widgets').

The warning states:

Removing WP_Customize_Manager::remove_panel manually will cause PHP warnings. Use the customize_loaded_components filter instead.

Apparently it was added because of some php and javascript errors that seemed to be occurring in WordPress releases from 4.0 to 4.5, when plugins and/or themes were giving contradictory orders, like one trying to modify the widgets panel after another had removed it before.

The warning suggestion, however, is not valid for themes, since customize_loaded_components is fired before them. So this hook called from a theme just won't have place in the right moment of the processing queue and will have no effect.

For 'nav_menus' there is this workaround, which doesn't seem to work for widgets:

$wp_customize->get_panel( 'nav_menus' )->active_callback = '__return_false';

There might be a method to remove the widgets panel effectively without the warning getting printed. But for a general, public usage, it just seems to be against the best practices to remove any of those core components from within a theme.

  • Thank you for the suggestion! I appreciate that. However, does that differ from what I'm currently doing, detailed in my original question, which generates a php warning? Looks exactly the same to me. – Scott Fennell May 2 '16 at 17:09
  • @ScottFennell, I'm sorry, I completely misread the question. After reading the title, I thought you were actually using customize_loaded_components. I have updated my answer. – Luis Sanz May 2 '16 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.