0

I'm trying to add a checkbox to the "Menus" panel in the customizer, but for some reason, it's not showing up. If I try changing it from "nav_menus" to "title_tagline," or "colors," the checkbox shows up just fine. What could be preventing it from showing up on the "Menus" panel?

// add custom options to the Customizer
function nssra_customizer_options($wp_customize) {
    // add "menu primary flex" checkbox setting
    $wp_customize->add_setting("menu_primary_flex", array(
        "capability"        => "edit_theme_options",
        "default"           => "1",
        "sanitize_callback" => "nssra_sanitize_checkbox",
    ));

    // add "menu primary flex" checkbox control
    $wp_customize->add_control("menu_primary_flex", array(
        "label"    => __("Stretch the primary menu to fill the available space.", "nssra"),
        "section"  => "nav_menus",
        "settings" => "menu_primary_flex",
        "std"      => "1",
        "type"     => "checkbox",
        "priority" => 1,
    ));
}
add_action("customize_register", "nssra_customizer_options");

// sanitize checkbox fields
function nssra_sanitize_checkbox($input, $setting) {
    return sanitize_key($input) === "1" ? 1 : 0;
}
  • I believe the section is just nav, not nav_menus. – Nathan Johnson Feb 13 '18 at 17:48
5

You cannot add any control/setting to nav_menus because that's not a section that's a panel. And control/setting can be added to the section only. So you have to create a section first under nav_menus panel then add your control/setting to that section. Check the following code

function nssra_customizer_options( $wp_customize ) {
    // add a custom section
    $wp_customize->add_section( 'nav_menus_custom', array(
        'title' => __( 'Custom Section Title', 'nssra' ),
        'panel' => 'nav_menus'
    ) );

    // add "menu primary flex" checkbox setting
    $wp_customize->add_setting( 'menu_primary_flex', array(
        'capability'        => 'edit_theme_options',
        'default'           => '1',
        'sanitize_callback' => 'nssra_sanitize_checkbox',
    ) );

    // add 'menu primary flex' checkbox control
    $wp_customize->add_control( 'menu_primary_flex', array(
        'label'    => __( 'Stretch the primary menu to fill the available space.', 'nssra' ),
        'section'  => 'nav_menus_custom',
        'settings' => 'menu_primary_flex',
        'std'      => '1',
        'type'     => 'checkbox',
        'priority' => 1,
    ));
}
add_action( 'customize_register', 'nssra_customizer_options' );

// sanitize checkbox fields
function nssra_sanitize_checkbox( $input, $setting ) {
    return sanitize_key( $input ) === '1' ? 1 : 0;
}

Update

You have to add section description to create the Menu Locations like appearance and if you want to change the section location then you can set priority. priority => 5 will place the custom section before Menu Locations, if you do this then you may have to adjust some styling. Please check the code below for updated section config.

$wp_customize->add_section( 'nav_menus_custom', array(
    'title'       => __( 'Custom Section Title', 'nssra' ),
    'panel'       => 'nav_menus',
    'description' => __( 'Section description goes here.', 'nssra' ),
    'priority'    => 5
) );
| improve this answer | |
  • Thanks, that makes sense. Quick follow up; is it possible to add text similar to the "Menu Locations" header before the section button? My custom section looks like it's part of the previous section because it lacks that style of heading. – JacobTheDev Feb 13 '18 at 19:25
  • Please check the update. – obiPlabon Feb 13 '18 at 20:36
0

Adding the following here in case you got lost along the way like I did. The solution by @odiPlabon is correct for the "nav_menus" panel. I came looking to add settings the individual menu sections e.g. Theme menus like the "Primary Menu" or "Secondary Menu".

For those as lost as I was the 'section' you're looking for is "nav_menu[$n]". Where $n is the term_id of the navigation menu.

You can see the term_id in the query parameters of the url in the address bar for the currently viewed menu – on the Wordpress Admin > Appearance > Menus page i.e "?action=edit&menu=43" <-- 43 being the term_id

$menus = wp_get_nav_menus();
if ( ! $menus && ! is_iterable($menus)) :
    return;
endif;

        foreach ($menus as $menu) :
            //
            $wp_customize->add_setting(
                    'menu_postition_' . $menu->term_id, // your setting name
                    array(
                        'settings' => 'nav_menu[' . $menu->term_id . ']',
                        'default' => 'menu-static',
                    )
            );
            //
            $wp_customize->add_control(
                    'menu_postition_' . $menu->term_id, // your setting name
                    array(
                        'type' => 'select',
                        'label' => 'Menu Position:',
                        'section' => 'nav_menu[' . $menu->term_id . ']',
                        'choices' => array(
                            'menu-static' => 'Normal',
                            'menu-fixed-top' => 'Fixed Top',
                            'menu-sticky-top' => 'Sticky Top',
                            'priority' => 20,
                        ),
                    )
            );
        endforeach;
| improve this answer | |

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.