Is it possible to change the priority of the Menus customizer section so that it is directly under the Site Identity section?
1 Answer
Update: I was curious and just searched this site for get_panel method and that seems to be the way to get and modify the panel instance. E.g. this question "How to access the nav_menus panel with the customizer api?" seems related. There @WestonRuter does change the priority of the Menus panel, but without applying add_panel()
and using the $wp_customize
instance as the input parameter in the customize_register
callback. I just checked and it's in fact passed on, because of do_action( 'customize_register', $this );
in WP_Customize_Manager::wp_loaded()
. Previously I had looked at:
add_action( 'customize_register', array( $this, 'register_controls' ) );
where:
/**
* Register some default controls.
*
* @since 3.4.0
*/
public function register_controls() {
had no input parameter.
I've therefore adjusted the answer below by removing the add_panel()
and by using the $wp_customize
as input parameter for the customize_register
callback, instead of function() use ( &$wp_customize )
.
I dug through the Customizer classes and tested various things. This approach seems to work:
/**
* Change priority and title for the 'Menus' panel in the Customizer
*/
add_action( 'customize_register', function( \WP_Customize_Manager $wp_customize )
{
// Get the 'Menus' panel instance
$mypanel = $wp_customize->get_panel( 'nav_menus');
if( $mypanel instanceof \WP_Customize_Nav_Menus_Panel )
{
// Adjust to your needs:
$mypanel ->priority = 21;
$mypanel ->title = 'My Great Menus!';
}
}, 12 );
but I've no idea how people do this in general or if it's the way to go ;-)
We use the priority of 21 here, since the Site Identity section has priority of 20.
In our case we have a panel, not section, but within the WP_Customize_Manager::prepare_controls()
the sections and panels are combined and ordered by priority via usort
and WP_Customize_Manager::_cmp_priority()
.
Here we can see the changes:
Before:
After:
We can make this more dynamic, by fetching the priority of the Site Identity section:
/**
* Change priority and title for the 'Menus' panel in the Customizer
*/
add_action( 'customize_register', function( \WP_Customize_Manager $wp_customize )
{
// Get the 'Menus' panel instance
$mypanel = $wp_customize->get_panel( 'nav_menus' );
// Get the 'Site Identity' section instance
$mysection = $wp_customize->get_section( 'title_tagline' );
if(
$mypanel instanceof \WP_Customize_Nav_Menus_Panel
&& $mysection instanceof \WP_Customize_Section
) {
// Adjust to your needs
$mypanel->priority = $mysection->priority + 1;
$mypanel->title = 'My Great Menus!';
}
}, 12 );