2

Is there a way to remove the "Menu's" or in dev language nav_menus from the customizer?

I have tried $wp_customize->remove_section('nav_menus'); but that did not work.

$wp_customize->remove_section('nav_menu'); (removed s) did also not work.

$wp_customize->remove_section('menus'); did also not work.

$wp_customize->remove_section('wp_nav_menus'); didn't work either.

Please help me out here!

5
  • Where did you add this code ? – Sumit Jun 4 '16 at 13:33
  • @Sumit I have tried adding it at the top of function.php, at the bottom and in the middle. None worked. I mainly tried adding the nav_menus. – J. Doe Jun 4 '16 at 13:35
  • Are you there, @Sumit? – J. Doe Jun 4 '16 at 14:23
  • Yes this is a good tricky question since this filter does not work in theme. I am busy a bit will try to find a solution later. Hope someone else may know this ;) – Sumit Jun 4 '16 at 14:37
  • @Sumit Okay, no problem! – J. Doe Jun 4 '16 at 14:38
3

This is panel not section so remove_section() will not work in this case. However you can use function remove_panel() but it will trigger a notice saying

Notice: WP_Customize_Manager::remove_panel was called incorrectly Removing nav_menus manually will cause PHP warnings. Use the customize_loaded_components filter instead. Please see Debugging in WordPress for more information. (This message was added in version 4.5.) in \wp-includes\functions.php on line 3897

Now customize_loaded_components filter says

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.

So there is no straight way for removing menu section from customizer and using theme but if you like then I found a solution (May be not so good) which works without any warnings and errors.

In class WP_Customize_Nav_Menus constructor check for

if ( ! current_user_can( 'edit_theme_options' ) ) {
    return;
}

And then register all the action/filters for nave menu section thus removing all those filter/action can do the trick. But for removing those filters/action we need the same object which was used to register them. And yes we have it in customize_register action callback.

You can add this code to your theme functions.php

add_action('customize_register', function ( $WP_Customize_Manager ){
    //check if WP_Customize_Nav_Menus object exist
    if (isset($WP_Customize_Manager->nav_menus) && is_object($WP_Customize_Manager->nav_menus)) {

        //Remove all the filters/actions resiterd in WP_Customize_Nav_Menus __construct
        remove_filter( 'customize_refresh_nonces', array( $WP_Customize_Manager->nav_menus, 'filter_nonces' ) );
        remove_action( 'wp_ajax_load-available-menu-items-customizer', array( $WP_Customize_Manager->nav_menus, 'ajax_load_available_items' ) );
        remove_action( 'wp_ajax_search-available-menu-items-customizer', array( $WP_Customize_Manager->nav_menus, 'ajax_search_available_items' ) );
        remove_action( 'customize_controls_enqueue_scripts', array( $WP_Customize_Manager->nav_menus, 'enqueue_scripts' ) );
        remove_action( 'customize_register', array( $WP_Customize_Manager->nav_menus, 'customize_register' ), 11 );
        remove_filter( 'customize_dynamic_setting_args', array( $WP_Customize_Manager->nav_menus, 'filter_dynamic_setting_args' ), 10, 2 );
        remove_filter( 'customize_dynamic_setting_class', array( $WP_Customize_Manager->nav_menus, 'filter_dynamic_setting_class' ), 10, 3 );
        remove_action( 'customize_controls_print_footer_scripts', array( $WP_Customize_Manager->nav_menus, 'print_templates' ) );
        remove_action( 'customize_controls_print_footer_scripts', array( $WP_Customize_Manager->nav_menus, 'available_items_template' ) );
        remove_action( 'customize_preview_init', array( $WP_Customize_Manager->nav_menus, 'customize_preview_init' ) );
        remove_filter( 'customize_dynamic_partial_args', array( $WP_Customize_Manager->nav_menus, 'customize_dynamic_partial_args' ), 10, 2 );

    }
}, -1); //Give it a lowest priority so we can remove it on right time

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.