';
} else {
$_menu_object = wp_get_nav_menu_object( $_nav_menu_selected_id );
$nav_menu_selected_title = $_menu_object->name;
}
}
// Update menu items.
if ( ! is_wp_error( $_menu_object ) ) {
$messages = array_merge( $messages, wp_nav_menu_update_menu_items( $_nav_menu_selected_id, $nav_menu_selected_title ) );
// If the menu ID changed, redirect to the new URL.
if ( $nav_menu_selected_id !== $_nav_menu_selected_id ) {
wp_redirect( admin_url( 'nav-menus.php?menu=' . (int) $_nav_menu_selected_id ) );
exit;
}
}
}
break;
case 'locations':
if ( ! $num_locations ) {
wp_redirect( admin_url( 'nav-menus.php' ) );
exit;
}
add_filter( 'screen_options_show_screen', '__return_false' );
if ( isset( $_POST['menu-locations'] ) ) {
check_admin_referer( 'save-menu-locations' );
$new_menu_locations = array_map( 'absint', $_POST['menu-locations'] );
$menu_locations = array_merge( $menu_locations, $new_menu_locations );
// Set menu locations.
set_theme_mod( 'nav_menu_locations', $menu_locations );
$messages[] = '
' . __( 'Menu locations updated.' ) . '
';
}
break;
}
// Get all nav menus.
$nav_menus = wp_get_nav_menus();
$menu_count = count( $nav_menus );
// Are we on the add new screen?
$add_new_screen = ( isset( $_GET['menu'] ) && 0 === (int) $_GET['menu'] ) ? true : false;
$locations_screen = ( isset( $_GET['action'] ) && 'locations' === $_GET['action'] ) ? true : false;
$page_count = wp_count_posts( 'page' );
/*
* If we have one theme location, and zero menus, we take them right
* into editing their first menu.
*/
if ( 1 === count( get_registered_nav_menus() ) && ! $add_new_screen
&& empty( $nav_menus ) && ! empty( $page_count->publish )
) {
$one_theme_location_no_menus = true;
} else {
$one_theme_location_no_menus = false;
}
$nav_menus_l10n = array(
'oneThemeLocationNoMenus' => $one_theme_location_no_menus,
'moveUp' => __( 'Move up one' ),
'moveDown' => __( 'Move down one' ),
'moveToTop' => __( 'Move to the top' ),
/* translators: %s: Previous item name. */
'moveUnder' => __( 'Move under %s' ),
/* translators: %s: Previous item name. */
'moveOutFrom' => __( 'Move out from under %s' ),
/* translators: %s: Previous item name. */
'under' => __( 'Under %s' ),
/* translators: %s: Previous item name. */
'outFrom' => __( 'Out from under %s' ),
/* translators: 1: Item name, 2: Item position, 3: Total number of items. */
'menuFocus' => __( '%1$s. Menu item %2$d of %3$d.' ),
/* translators: 1: Item name, 2: Item position, 3: Parent item name. */
'subMenuFocus' => __( '%1$s. Sub item number %2$d under %3$s.' ),
/* translators: %s: Item name. */
'menuItemDeletion' => __( 'item %s' ),
/* translators: %s: Item name. */
'itemsDeleted' => __( 'Deleted menu item: %s.' ),
'itemAdded' => __( 'Menu item added' ),
'itemRemoved' => __( 'Menu item removed' ),
'movedUp' => __( 'Menu item moved up' ),
'movedDown' => __( 'Menu item moved down' ),
'movedTop' => __( 'Menu item moved to the top' ),
'movedLeft' => __( 'Menu item moved out of submenu' ),
'movedRight' => __( 'Menu item is now a sub-item' ),
);
wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n );
/*
* Redirect to add screen if there are no menus and this users has either zero,
* or more than 1 theme locations.
*/
if ( 0 === $menu_count && ! $add_new_screen && ! $one_theme_location_no_menus ) {
wp_redirect( admin_url( 'nav-menus.php?action=edit&menu=0' ) );
}
// Get recently edited nav menu.
$recently_edited = absint( get_user_option( 'nav_menu_recently_edited' ) );
if ( empty( $recently_edited ) && is_nav_menu( $nav_menu_selected_id ) ) {
$recently_edited = $nav_menu_selected_id;
}
// Use $recently_edited if none are selected.
if ( empty( $nav_menu_selected_id ) && ! isset( $_GET['menu'] ) && is_nav_menu( $recently_edited ) ) {
$nav_menu_selected_id = $recently_edited;
}
// On deletion of menu, if another menu exists, show it.
if ( ! $add_new_screen && $menu_count > 0 && isset( $_GET['action'] ) && 'delete' === $_GET['action'] ) {
$nav_menu_selected_id = $nav_menus[0]->term_id;
}
// Set $nav_menu_selected_id to 0 if no menus.
if ( $one_theme_location_no_menus ) {
$nav_menu_selected_id = 0;
} elseif ( empty( $nav_menu_selected_id ) && ! empty( $nav_menus ) && ! $add_new_screen ) {
// If we have no selection yet, and we have menus, set to the first one in the list.
$nav_menu_selected_id = $nav_menus[0]->term_id;
}
// Update the user's setting.
if ( $nav_menu_selected_id !== $recently_edited && is_nav_menu( $nav_menu_selected_id ) ) {
update_user_meta( $current_user->ID, 'nav_menu_recently_edited', $nav_menu_selected_id );
}
// If there's a menu, get its name.
if ( ! $nav_menu_selected_title && is_nav_menu( $nav_menu_selected_id ) ) {
$_menu_object = wp_get_nav_menu_object( $nav_menu_selected_id );
$nav_menu_selected_title = ! is_wp_error( $_menu_object ) ? $_menu_object->name : '';
}
// Generate truncated menu names.
foreach ( (array) $nav_menus as $key => $_nav_menu ) {
$nav_menus[ $key ]->truncated_name = wp_html_excerpt( $_nav_menu->name, 40, '…' );
}
// Retrieve menu locations.
if ( current_theme_supports( 'menus' ) ) {
$locations = get_registered_nav_menus();
$menu_locations = get_nav_menu_locations();
}
/*
* Ensure the user will be able to scroll horizontally
* by adding a class for the max menu depth.
*
* @global int $_wp_nav_menu_max_depth
*/
global $_wp_nav_menu_max_depth;
$_wp_nav_menu_max_depth = 0;
// Calling wp_get_nav_menu_to_edit generates $_wp_nav_menu_max_depth.
if ( is_nav_menu( $nav_menu_selected_id ) ) {
$menu_items = wp_get_nav_menu_items( $nav_menu_selected_id, array( 'post_status' => 'any' ) );
$edit_markup = wp_get_nav_menu_to_edit( $nav_menu_selected_id );
}
/**
* @global int $_wp_nav_menu_max_depth
*
* @param string $classes
* @return string
*/
function wp_nav_menu_max_depth( $classes ) {
global $_wp_nav_menu_max_depth;
return "$classes menu-max-depth-$_wp_nav_menu_max_depth";
}
add_filter( 'admin_body_class', 'wp_nav_menu_max_depth' );
wp_nav_menu_setup();
wp_initial_nav_menu_meta_boxes();
if ( ! current_theme_supports( 'menus' ) && ! $num_locations ) {
$messages[] = '
' . sprintf(
/* translators: %s: URL to Widgets screen. */
__( 'Your theme does not natively support menus, but you can use them in sidebars by adding a “Navigation Menu” widget on the Widgets screen.' ),
admin_url( 'widgets.php' )
) . '
';
}
if ( ! $locations_screen ) : // Main tab.
$overview = '
' . __( 'This screen is used for managing your navigation menus.' ) . '
';
$overview .= '
' . sprintf(
/* translators: 1: URL to Widgets screen, 2 and 3: The names of the default themes. */
__( 'Menus can be displayed in locations defined by your theme, even used in sidebars by adding a “Navigation Menu” widget on the Widgets screen. If your theme does not support the navigation menus feature (the default themes, %2$s and %3$s, do), you can learn about adding this support by following the documentation link to the side.' ),
admin_url( 'widgets.php' ),
'Twenty Twenty',
'Twenty Twenty-One'
) . '
';
$overview .= '
' . __( 'From this screen you can:' ) . '
';
$overview .= '
' . __( 'Create, edit, and delete menus' ) . '
';
$overview .= '
' . __( 'Add, organize, and modify individual menu items' ) . '
' . __( 'The menu management box at the top of the screen is used to control which menu is opened in the editor below.' ) . '
';
$menu_management .= '
' . __( 'To edit an existing menu, choose a menu from the dropdown and click Select' ) . '
';
$menu_management .= '
' . __( 'If you have not yet created any menus, click the ’create a new menu’ link to get started' ) . '
';
$menu_management .= '
' . __( 'You can assign theme locations to individual menus by selecting the desired settings at the bottom of the menu editor. To assign menus to all theme locations at once, visit the Manage Locations tab at the top of the screen.' ) . '
' . __( 'Each navigation menu may contain a mix of links to pages, categories, custom URLs or other content types. Menu links are added by selecting items from the expanding boxes in the left-hand column below.' ) . '
';
$editing_menus .= '
' . __( 'Clicking the arrow to the right of any menu item in the editor will reveal a standard group of settings. Additional settings such as link target, CSS classes, link relationships, and link descriptions can be enabled and disabled via the Screen Options tab.' ) . '
';
$editing_menus .= '
' . __( 'Add one or several items at once by selecting the checkbox next to each item and clicking Add to Menu' ) . '
';
$editing_menus .= '
' . __( 'To add a custom link, expand the Custom Links section, enter a URL and link text, and click Add to Menu' ) . '
';
$editing_menus .= '
' . __( 'To reorganize menu items, drag and drop items with your mouse or use your keyboard. Drag or move a menu item a little to the right to make it a submenu' ) . '
';
$editing_menus .= '
' . __( 'Delete a menu item by expanding it and clicking the Remove link' ) . '
' . __( 'This screen is used for globally assigning menus to locations defined by your theme.' ) . '
';
$locations_overview .= '
' . __( 'To assign menus to one or more theme locations, select a menu from each location’s dropdown. When you are finished, click Save Changes' ) . '
';
$locations_overview .= '
' . __( 'To edit a menu currently assigned to a theme location, click the adjacent ’Edit’ link' ) . '
';
$locations_overview .= '
' . __( 'To add a new menu instead of assigning an existing one, click the ’Use new menu’ link. Your new menu will be automatically assigned to that theme location' ) . '
'
);
// Get the admin header.
require_once ABSPATH . 'wp-admin/admin-header.php';
?>
'menu_locations' ) : array( 'panel' => 'nav_menus' );
printf(
' %2$s',
esc_url(
add_query_arg(
array(
array( 'autofocus' => $focus ),
'return' => urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ),
),
admin_url( 'customize.php' )
)
),
__( 'Manage with Live Preview' )
);
endif;
$nav_tab_active_class = '';
$nav_aria_current = '';
if ( ! isset( $_GET['action'] ) || isset( $_GET['action'] ) && 'locations' !== $_GET['action'] ) {
$nav_tab_active_class = ' nav-tab-active';
$nav_aria_current = ' aria-current="page"';
}
?>
' . __( 'Your theme supports one menu. Select which menu you would like to use.' ) . '';
} else {
echo '
' . sprintf(
/* translators: %s: Number of menus. */
_n(
'Your theme supports %s menu. Select which menu appears in each location.',
'Your theme supports %s menus. Select which menu appears in each location.',
$num_locations
),
number_format_i18n( $num_locations )
) . '
';
}
?>
create a new menu. Do not forget to save your changes!' ),
esc_url(
add_query_arg(
array(
'action' => 'edit',
'menu' => 0,
),
admin_url( 'nav-menus.php' )
)
)
);
?>