* Core Navigation Menu API
/** Walker_Nav_Menu_Edit class */
require_once ABSPATH . 'wp-admin/includes/class-walker-nav-menu-edit.php';
/** Walker_Nav_Menu_Checklist class */
require_once ABSPATH . 'wp-admin/includes/class-walker-nav-menu-checklist.php';
* Prints the appropriate response to a menu quick search.
* @param array $request The unsanitized request values.
function _wp_ajax_menu_quick_search( $request = array() ) {
$type = isset( $request['type'] ) ? $request['type'] : '';
$object_type = isset( $request['object_type'] ) ? $request['object_type'] : '';
$query = isset( $request['q'] ) ? $request['q'] : '';
$response_format = isset( $request['response-format'] ) ? $request['response-format'] : '';
if ( ! $response_format || ! in_array( $response_format, array( 'json', 'markup' ), true ) ) {
$response_format = 'json';
if ( 'markup' === $response_format ) {
$args['walker'] = new Walker_Nav_Menu_Checklist;
if ( 'get-post-item' === $type ) {
if ( post_type_exists( $object_type ) ) {
if ( isset( $request['ID'] ) ) {
$object_id = (int) $request['ID'];
if ( 'markup' === $response_format ) {
echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( get_post( $object_id ) ) ), 0, (object) $args );
} elseif ( 'json' === $response_format ) {
'post_title' => get_the_title( $object_id ),
'post_type' => get_post_type( $object_id ),
} elseif ( taxonomy_exists( $object_type ) ) {
if ( isset( $request['ID'] ) ) {
$object_id = (int) $request['ID'];
if ( 'markup' === $response_format ) {
echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( get_term( $object_id, $object_type ) ) ), 0, (object) $args );
} elseif ( 'json' === $response_format ) {
$post_obj = get_term( $object_id, $object_type );
'post_title' => $post_obj->name,
'post_type' => $object_type,
} elseif ( preg_match( '/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches ) ) {
if ( 'posttype' === $matches[1] && get_post_type_object( $matches[2] ) ) {
$post_type_obj = _wp_nav_menu_meta_box_object( get_post_type_object( $matches[2] ) );
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'post_type' => $matches[2],
if ( isset( $post_type_obj->_default_query ) ) {
$args = array_merge( $args, (array) $post_type_obj->_default_query );
$search_results_query = new WP_Query( $args );
if ( ! $search_results_query->have_posts() ) {
while ( $search_results_query->have_posts() ) {
$post = $search_results_query->next_post();
if ( 'markup' === $response_format ) {
echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( get_post( $var_by_ref ) ) ), 0, (object) $args );
} elseif ( 'json' === $response_format ) {
'post_title' => get_the_title( $post->ID ),
'post_type' => $matches[2],
} elseif ( 'taxonomy' === $matches[1] ) {
'taxonomy' => $matches[2],
if ( empty( $terms ) || is_wp_error( $terms ) ) {
foreach ( (array) $terms as $term ) {
if ( 'markup' === $response_format ) {
echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args );
} elseif ( 'json' === $response_format ) {
'post_title' => $term->name,
'post_type' => $matches[2],
* Register nav menu meta boxes and advanced menu items.
function wp_nav_menu_setup() {
wp_nav_menu_post_type_meta_boxes();
add_meta_box( 'add-custom-links', __( 'Custom Links' ), 'wp_nav_menu_item_link_meta_box', 'nav-menus', 'side', 'default' );
wp_nav_menu_taxonomy_meta_boxes();
// Register advanced menu items (columns).
add_filter( 'manage_nav-menus_columns', 'wp_nav_menu_manage_columns' );
// If first time editing, disable advanced items by default.
if ( false === get_user_option( 'managenav-menuscolumnshidden' ) ) {
$user = wp_get_current_user();
'managenav-menuscolumnshidden',
* Limit the amount of meta boxes to pages, posts, links, and categories for first time users.
* @global array $wp_meta_boxes
function wp_initial_nav_menu_meta_boxes() {
if ( get_user_option( 'metaboxhidden_nav-menus' ) !== false || ! is_array( $wp_meta_boxes ) ) {
$initial_meta_boxes = array( 'add-post-type-page', 'add-post-type-post', 'add-custom-links', 'add-category' );
$hidden_meta_boxes = array();
foreach ( array_keys( $wp_meta_boxes['nav-menus'] ) as $context ) {
foreach ( array_keys( $wp_meta_boxes['nav-menus'][ $context ] ) as $priority ) {
foreach ( $wp_meta_boxes['nav-menus'][ $context ][ $priority ] as $box ) {
if ( in_array( $box['id'], $initial_meta_boxes, true ) ) {
$hidden_meta_boxes[] = $box['id'];
$user = wp_get_current_user();
update_user_option( $user->ID, 'metaboxhidden_nav-menus', $hidden_meta_boxes, true );
* Creates meta boxes for any post type menu item..
function wp_nav_menu_post_type_meta_boxes() {
$post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'object' );
foreach ( $post_types as $post_type ) {
* Filters whether a menu items meta box will be added for the current
* If a falsey value is returned instead of an object, the menu items
* meta box for the current meta box object will not be added.
* @param WP_Post_Type|false $post_type The current object to add a menu items
$post_type = apply_filters( 'nav_menu_meta_box_object', $post_type );
// Give pages a higher priority.
$priority = ( 'page' === $post_type->name ? 'core' : 'default' );
add_meta_box( "add-post-type-{$id}", $post_type->labels->name, 'wp_nav_menu_item_post_type_meta_box', 'nav-menus', 'side', $priority, $post_type );
* Creates meta boxes for any taxonomy menu item.
function wp_nav_menu_taxonomy_meta_boxes() {
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'object' );
foreach ( $taxonomies as $tax ) {
/** This filter is documented in wp-admin/includes/nav-menu.php */
$tax = apply_filters( 'nav_menu_meta_box_object', $tax );
add_meta_box( "add-{$id}", $tax->labels->name, 'wp_nav_menu_item_taxonomy_meta_box', 'nav-menus', 'side', 'default', $tax );
* Check whether to disable the Menu Locations meta box submit button and inputs.
* @since 5.3.1 The `$echo` parameter was added.
* @global bool $one_theme_location_no_menus to determine if no menus exist
* @param int|string $nav_menu_selected_id ID, name, or slug of the currently selected menu.
* @param bool $echo Whether to echo or just return the string.
* @return string|false Disabled attribute if at least one menu exists, false if not.
function wp_nav_menu_disabled_check( $nav_menu_selected_id, $echo = true ) {
global $one_theme_location_no_menus;
if ( $one_theme_location_no_menus ) {
return disabled( $nav_menu_selected_id, 0, $echo );
* Displays a meta box for the custom links menu item.
* @global int $_nav_menu_placeholder
* @global int|string $nav_menu_selected_id
function wp_nav_menu_item_link_meta_box() {
global $_nav_menu_placeholder, $nav_menu_selected_id;
$_nav_menu_placeholder = 0 > $_nav_menu_placeholder ? $_nav_menu_placeholder - 1 : -1;
<div class="customlinkdiv" id="customlinkdiv">
<input type="hidden" value="custom" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-type]" />
<p id="menu-item-url-wrap" class="wp-clearfix">
<label class="howto" for="custom-menu-item-url"><?php _e( 'URL' ); ?></label>
<input id="custom-menu-item-url" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-url]" type="text"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="code menu-item-textbox form-required" placeholder="https://" />
<p id="menu-item-name-wrap" class="wp-clearfix">
<label class="howto" for="custom-menu-item-name"><?php _e( 'Link Text' ); ?></label>
<input id="custom-menu-item-name" name="menu-item[<?php echo $_nav_menu_placeholder; ?>][menu-item-title]" type="text"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="regular-text menu-item-textbox" />
<p class="button-controls wp-clearfix">
<span class="add-to-menu">
<input type="submit"<?php wp_nav_menu_disabled_check( $nav_menu_selected_id ); ?> class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to Menu' ); ?>" name="add-custom-menu-item" id="submit-customlinkdiv" />
<span class="spinner"></span>
</div><!-- /.customlinkdiv -->
* Displays a meta box for a post type menu item.
* @global int $_nav_menu_placeholder
* @global int|string $nav_menu_selected_id
* @param string $object Not used.
* Post type menu item meta box arguments.
* @type string $id Meta box 'id' attribute.
* @type string $title Meta box title.
* @type callable $callback Meta box display callback.
* @type WP_Post_Type $args Extra meta box arguments (the post type object for this meta box).
function wp_nav_menu_item_post_type_meta_box( $object, $box ) {
global $_nav_menu_placeholder, $nav_menu_selected_id;
$post_type_name = $box['args']->name;
$post_type = get_post_type_object( $post_type_name );
$tab_name = $post_type_name . '-tab';
// Paginate browsing for large numbers of post objects.
$pagenum = isset( $_REQUEST[ $tab_name ] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
$offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
'posts_per_page' => $per_page,
'post_type' => $post_type_name,
'suppress_filters' => true,
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
if ( isset( $box['args']->_default_query ) ) {
$args = array_merge( $args, (array) $box['args']->_default_query );
* If we're dealing with pages, let's prioritize the Front Page,
* Posts Page and Privacy Policy Page at the top of the list.
$important_pages = array();
if ( 'page' === $post_type_name ) {
$suppress_page_ids = array();
// Insert Front Page or custom Home link.
$front_page = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_on_front' ) : 0;
if ( ! empty( $front_page ) ) {
$front_page_obj = get_post( $front_page );
$front_page_obj->front_or_home = true;
$important_pages[] = $front_page_obj;
$suppress_page_ids[] = $front_page_obj->ID;
$_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? (int) $_nav_menu_placeholder - 1 : -1;
$front_page_obj = (object) array(
'object_id' => $_nav_menu_placeholder,
'post_title' => _x( 'Home', 'nav menu home label' ),
'post_type' => 'nav_menu_item',
'url' => home_url( '/' ),
$important_pages[] = $front_page_obj;
$posts_page = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_for_posts' ) : 0;
if ( ! empty( $posts_page ) ) {
$posts_page_obj = get_post( $posts_page );
$posts_page_obj->posts_page = true;
$important_pages[] = $posts_page_obj;
$suppress_page_ids[] = $posts_page_obj->ID;
// Insert Privacy Policy Page.
$privacy_policy_page_id = (int) get_option( 'wp_page_for_privacy_policy' );
if ( ! empty( $privacy_policy_page_id ) ) {
$privacy_policy_page = get_post( $privacy_policy_page_id );
if ( $privacy_policy_page instanceof WP_Post && 'publish' === $privacy_policy_page->post_status ) {
$privacy_policy_page->privacy_policy_page = true;
$important_pages[] = $privacy_policy_page;
$suppress_page_ids[] = $privacy_policy_page->ID;
// Add suppression array to arguments for WP_Query.
if ( ! empty( $suppress_page_ids ) ) {
$args['post__not_in'] = $suppress_page_ids;
// @todo Transient caching of these results with proper invalidation on updating of a post of this type.
$get_posts = new WP_Query;
$posts = $get_posts->query( $args );
// Only suppress and insert when more than just suppression pages available.
if ( ! $get_posts->post_count ) {
if ( ! empty( $suppress_page_ids ) ) {
unset( $args['post__not_in'] );
$get_posts = new WP_Query;
$posts = $get_posts->query( $args );
echo '<p>' . __( 'No items.' ) . '</p>';
} elseif ( ! empty( $important_pages ) ) {
$posts = array_merge( $important_pages, $posts );
$num_pages = $get_posts->max_num_pages;
$page_links = paginate_links(
'item-type' => 'post_type',
'item-object' => $post_type_name,
'prev_text' => '<span aria-label="' . esc_attr__( 'Previous page' ) . '">' . __( '«' ) . '</span>',
'next_text' => '<span aria-label="' . esc_attr__( 'Next page' ) . '">' . __( '»' ) . '</span>',
'before_page_number' => '<span class="screen-reader-text">' . __( 'Page' ) . '</span> ',
if ( is_post_type_hierarchical( $post_type_name ) ) {
'parent' => 'post_parent',
$walker = new Walker_Nav_Menu_Checklist( $db_fields );
$current_tab = 'most-recent';
if ( isset( $_REQUEST[ $tab_name ] ) && in_array( $_REQUEST[ $tab_name ], array( 'all', 'search' ), true ) ) {
$current_tab = $_REQUEST[ $tab_name ];
if ( ! empty( $_REQUEST[ 'quick-search-posttype-' . $post_type_name ] ) ) {