Skip to content

Instantly share code, notes, and snippets.

@jakebresnehan
Last active August 29, 2015 14:23
Show Gist options
  • Save jakebresnehan/c11cd64c187cc9b48df8 to your computer and use it in GitHub Desktop.
Save jakebresnehan/c11cd64c187cc9b48df8 to your computer and use it in GitHub Desktop.
menu-control-wordpress-custromizer.php
/* Nav Menus */
$locations = get_registered_nav_menus();
$menus = wp_get_nav_menus();
$menu_locations = get_nav_menu_locations();
$num_locations = count( array_keys( $locations ) );
$wp_customize->add_section( 'nav', array(
'title' => __( 'Navigation' ),
'theme_supports' => 'menus',
'priority' => 40,
'description' => sprintf( _n('Your theme supports %s menu. Select which menu you would like to use.', 'Your theme supports %s menus. Select which menu appears in each location.', $num_locations ), number_format_i18n( $num_locations ) ) . "\n\n" . __('You can edit your menu content on the Menus screen in the Appearance section.'),
) );
if ( $menus ) {
$choices = array( 0 => __( '— Select —' ) );
foreach ( $menus as $menu ) {
$truncated_name = wp_html_excerpt( $menu->name, 40 );
$truncated_name = ( $truncated_name == $menu->name ) ? $menu->name : trim( $truncated_name ) . '…';
$choices[ $menu->term_id ] = $truncated_name;
}
foreach ( $locations as $location => $description ) {
$menu_setting_id = "nav_menu_locations[{$location}]";
$wp_customize->add_setting( $menu_setting_id, array(
'sanitize_callback' => 'absint',
'theme_supports' => 'menus',
) );
$wp_customize->add_control( $menu_setting_id, array(
'label' => $description,
'section' => 'nav',
'type' => 'select',
'choices' => $choices,
) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment