3 * Navigation Menu functions
6 * @subpackage Nav_Menus
11 * Returns a navigation menu object.
15 * @param string $menu Menu ID, slug, or name - or the menu object.
16 * @return object|false False if $menu param isn't supplied or term does not exist, menu object if successful.
18 function wp_get_nav_menu_object( $menu ) {
21 if ( is_object( $menu ) ) {
25 if ( $menu && ! $menu_obj ) {
26 $menu_obj = get_term( $menu, 'nav_menu' );
29 $menu_obj = get_term_by( 'slug', $menu, 'nav_menu' );
33 $menu_obj = get_term_by( 'name', $menu, 'nav_menu' );
37 if ( ! $menu_obj || is_wp_error( $menu_obj ) ) {
42 * Filter the nav_menu term retrieved for wp_get_nav_menu_object().
46 * @param object|false $menu_obj Term from nav_menu taxonomy, or false if nothing had been found.
47 * @param string $menu The menu ID, slug, or name passed to wp_get_nav_menu_object().
49 return apply_filters( 'wp_get_nav_menu_object', $menu_obj, $menu );
53 * Check if the given ID is a navigation menu.
55 * Returns true if it is; false otherwise.
59 * @param int|string $menu The menu to check (ID, slug, or name).
60 * @return bool Whether the menu exists.
62 function is_nav_menu( $menu ) {
66 $menu_obj = wp_get_nav_menu_object( $menu );
70 ! is_wp_error( $menu_obj ) &&
71 ! empty( $menu_obj->taxonomy ) &&
72 'nav_menu' == $menu_obj->taxonomy
80 * Register navigation menus for a theme.
84 * @global array $_wp_registered_nav_menus
86 * @param array $locations Associative array of menu location identifiers (like a slug) and descriptive text.
88 function register_nav_menus( $locations = array() ) {
89 global $_wp_registered_nav_menus;
91 add_theme_support( 'menus' );
93 $_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations );
97 * Unregisters a navigation menu for a theme.
99 * @global array $_wp_registered_nav_menus
101 * @param string $location The menu location identifier.
102 * @return bool True on success, false on failure.
104 function unregister_nav_menu( $location ) {
105 global $_wp_registered_nav_menus;
107 if ( is_array( $_wp_registered_nav_menus ) && isset( $_wp_registered_nav_menus[$location] ) ) {
108 unset( $_wp_registered_nav_menus[$location] );
109 if ( empty( $_wp_registered_nav_menus ) ) {
110 _remove_theme_support( 'menus' );
118 * Register a navigation menu for a theme.
122 * @param string $location Menu location identifier, like a slug.
123 * @param string $description Menu location descriptive text.
125 function register_nav_menu( $location, $description ) {
126 register_nav_menus( array( $location => $description ) );
129 * Returns an array of all registered navigation menus in a theme
133 * @global array $_wp_registered_nav_menus
137 function get_registered_nav_menus() {
138 global $_wp_registered_nav_menus;
139 if ( isset( $_wp_registered_nav_menus ) )
140 return $_wp_registered_nav_menus;
145 * Returns an array with the registered navigation menu locations and the menu assigned to it
151 function get_nav_menu_locations() {
152 $locations = get_theme_mod( 'nav_menu_locations' );
153 return ( is_array( $locations ) ) ? $locations : array();
157 * Whether a registered nav menu location has a menu assigned to it.
161 * @param string $location Menu location identifier.
162 * @return bool Whether location has a menu.
164 function has_nav_menu( $location ) {
165 $has_nav_menu = false;
167 $registered_nav_menus = get_registered_nav_menus();
168 if ( isset( $registered_nav_menus[ $location ] ) ) {
169 $locations = get_nav_menu_locations();
170 $has_nav_menu = ! empty( $locations[ $location ] );
174 * Filter whether a nav menu is assigned to the specified location.
178 * @param bool $has_nav_menu Whether there is a menu assigned to a location.
179 * @param string $location Menu location.
181 return apply_filters( 'has_nav_menu', $has_nav_menu, $location );
185 * Determine whether the given ID is a nav menu item.
189 * @param int $menu_item_id The ID of the potential nav menu item.
190 * @return bool Whether the given ID is that of a nav menu item.
192 function is_nav_menu_item( $menu_item_id = 0 ) {
193 return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' == get_post_type( $menu_item_id ) ) );
197 * Create a Navigation Menu.
201 * @param string $menu_name Menu name.
202 * @return int|WP_Error Menu ID on success, WP_Error object on failure.
204 function wp_create_nav_menu( $menu_name ) {
205 return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
209 * Delete a Navigation Menu.
213 * @param string $menu Menu ID, slug, or name.
214 * @return bool|WP_Error True on success, false or WP_Error object on failure.
216 function wp_delete_nav_menu( $menu ) {
217 $menu = wp_get_nav_menu_object( $menu );
221 $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
222 if ( ! empty( $menu_objects ) ) {
223 foreach ( $menu_objects as $item ) {
224 wp_delete_post( $item );
228 $result = wp_delete_term( $menu->term_id, 'nav_menu' );
230 // Remove this menu from any locations.
231 $locations = get_nav_menu_locations();
232 foreach ( $locations as $location => $menu_id ) {
233 if ( $menu_id == $menu->term_id )
234 $locations[ $location ] = 0;
236 set_theme_mod( 'nav_menu_locations', $locations );
238 if ( $result && !is_wp_error($result) )
241 * Fires after a navigation menu has been successfully deleted.
245 * @param int $term_id ID of the deleted menu.
247 do_action( 'wp_delete_nav_menu', $menu->term_id );
253 * Save the properties of a menu or create a new menu with those properties.
257 * @param int $menu_id The ID of the menu or "0" to create a new menu.
258 * @param array $menu_data The array of menu data.
259 * @return int|WP_Error Menu ID on success, WP_Error object on failure.
261 function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
262 $menu_id = (int) $menu_id;
264 $_menu = wp_get_nav_menu_object( $menu_id );
267 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),
268 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),
269 'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),
273 // double-check that we're not going to have one menu take the name of another
274 $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
276 $_possible_existing &&
277 ! is_wp_error( $_possible_existing ) &&
278 isset( $_possible_existing->term_id ) &&
279 $_possible_existing->term_id != $menu_id
281 return new WP_Error( 'menu_exists', sprintf( __('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html( $menu_data['menu-name'] ) ) );
283 // menu doesn't already exist, so create a new menu
284 if ( ! $_menu || is_wp_error( $_menu ) ) {
285 $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
288 return new WP_Error( 'menu_exists', sprintf( __('The menu name <strong>%s</strong> conflicts with another menu name. Please try another.'), esc_html( $menu_data['menu-name'] ) ) );
290 $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );
292 if ( is_wp_error( $_menu ) )
296 * Fires after a navigation menu is successfully created.
300 * @param int $term_id ID of the new menu.
301 * @param array $menu_data An array of menu data.
303 do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
305 return (int) $_menu['term_id'];
308 if ( ! $_menu || ! isset( $_menu->term_id ) )
311 $menu_id = (int) $_menu->term_id;
313 $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
315 if ( is_wp_error( $update_response ) )
316 return $update_response;
318 $menu_id = (int) $update_response['term_id'];
321 * Fires after a navigation menu has been successfully updated.
325 * @param int $menu_id ID of the updated menu.
326 * @param array $menu_data An array of menu data.
328 do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
333 * Save the properties of a menu item or create a new one.
337 * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
338 * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
339 * @param array $menu_item_data The menu item's data.
340 * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
342 function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) {
343 $menu_id = (int) $menu_id;
344 $menu_item_db_id = (int) $menu_item_db_id;
346 // make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects
347 if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) )
348 return new WP_Error( 'update_nav_menu_item_failed', __( 'The given object ID is not that of a menu item.' ) );
350 $menu = wp_get_nav_menu_object( $menu_id );
352 if ( ! $menu && 0 !== $menu_id ) {
353 return new WP_Error( 'invalid_menu_id', __( 'Invalid menu ID.' ) );
356 if ( is_wp_error( $menu ) ) {
361 'menu-item-db-id' => $menu_item_db_id,
362 'menu-item-object-id' => 0,
363 'menu-item-object' => '',
364 'menu-item-parent-id' => 0,
365 'menu-item-position' => 0,
366 'menu-item-type' => 'custom',
367 'menu-item-title' => '',
368 'menu-item-url' => '',
369 'menu-item-description' => '',
370 'menu-item-attr-title' => '',
371 'menu-item-target' => '',
372 'menu-item-classes' => '',
373 'menu-item-xfn' => '',
374 'menu-item-status' => '',
377 $args = wp_parse_args( $menu_item_data, $defaults );
379 if ( 0 == $menu_id ) {
380 $args['menu-item-position'] = 1;
381 } elseif ( 0 == (int) $args['menu-item-position'] ) {
382 $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
383 $last_item = array_pop( $menu_items );
384 $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items );
387 $original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;
389 if ( 'custom' != $args['menu-item-type'] ) {
390 /* if non-custom menu item, then:
391 * use original object's URL
392 * blank default title to sync with original object's
395 $args['menu-item-url'] = '';
397 $original_title = '';
398 if ( 'taxonomy' == $args['menu-item-type'] ) {
399 $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
400 $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
401 } elseif ( 'post_type' == $args['menu-item-type'] ) {
403 $original_object = get_post( $args['menu-item-object-id'] );
404 $original_parent = (int) $original_object->post_parent;
405 $original_title = $original_object->post_title;
408 if ( $args['menu-item-title'] == $original_title )
409 $args['menu-item-title'] = '';
411 // hack to get wp to create a post object when too many properties are empty
412 if ( '' == $args['menu-item-title'] && '' == $args['menu-item-description'] )
413 $args['menu-item-description'] = ' ';
416 // Populate the menu item object
418 'menu_order' => $args['menu-item-position'],
420 'post_content' => $args['menu-item-description'],
421 'post_excerpt' => $args['menu-item-attr-title'],
422 'post_parent' => $original_parent,
423 'post_title' => $args['menu-item-title'],
424 'post_type' => 'nav_menu_item',
427 $update = 0 != $menu_item_db_id;
429 // New menu item. Default is draft status
432 $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
433 $menu_item_db_id = wp_insert_post( $post );
434 if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
435 return $menu_item_db_id;
438 // Associate the menu item with the menu term
439 // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
440 if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
441 wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
444 if ( 'custom' == $args['menu-item-type'] ) {
445 $args['menu-item-object-id'] = $menu_item_db_id;
446 $args['menu-item-object'] = 'custom';
449 $menu_item_db_id = (int) $menu_item_db_id;
451 update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']) );
452 update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', strval( (int) $args['menu-item-parent-id'] ) );
453 update_post_meta( $menu_item_db_id, '_menu_item_object_id', strval( (int) $args['menu-item-object-id'] ) );
454 update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']) );
455 update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']) );
457 $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) );
458 $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );
459 update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
460 update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
461 update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) );
464 update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
465 elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) )
466 delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
468 // Update existing menu item. Default is publish status
470 $post['ID'] = $menu_item_db_id;
471 $post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
472 wp_update_post( $post );
476 * Fires after a navigation menu item has been updated.
480 * @see wp_update_nav_menu_item()
482 * @param int $menu_id ID of the updated menu.
483 * @param int $menu_item_db_id ID of the updated menu item.
484 * @param array $args An array of arguments used to update a menu item.
486 do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
488 return $menu_item_db_id;
492 * Returns all navigation menu objects.
495 * @since 4.1.0 Default value of the 'orderby' argument was changed from 'none'
498 * @param array $args Optional. Array of arguments passed on to {@see get_terms()}.
499 * Default empty array.
500 * @return array Menu objects.
502 function wp_get_nav_menus( $args = array() ) {
503 $defaults = array( 'hide_empty' => false, 'orderby' => 'name' );
504 $args = wp_parse_args( $args, $defaults );
507 * Filter the navigation menu objects being returned.
513 * @param array $menus An array of menu objects.
514 * @param array $args An array of arguments used to retrieve menu objects.
516 return apply_filters( 'wp_get_nav_menus', get_terms( 'nav_menu', $args), $args );
520 * Sort menu items by the desired key.
525 * @global string $_menu_item_sort_prop
527 * @param object $a The first object to compare
528 * @param object $b The second object to compare
529 * @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b.
531 function _sort_nav_menu_items( $a, $b ) {
532 global $_menu_item_sort_prop;
534 if ( empty( $_menu_item_sort_prop ) )
537 if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) )
540 $_a = (int) $a->$_menu_item_sort_prop;
541 $_b = (int) $b->$_menu_item_sort_prop;
543 if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop )
545 elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop )
546 return $_a < $_b ? -1 : 1;
548 return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop );
552 * Return if a menu item is valid.
554 * @link https://core.trac.wordpress.org/ticket/13958
559 * @param object $item The menu item to check.
560 * @return bool False if invalid, otherwise true.
562 function _is_valid_nav_menu_item( $item ) {
563 return empty( $item->_invalid );
567 * Return all menu items of a navigation menu.
571 * @global string $_menu_item_sort_prop
572 * @staticvar array $fetched
574 * @param string $menu Menu name, ID, or slug.
575 * @param array $args Optional. Arguments to pass to {@see get_posts()}.
576 * @return false|array $items Array of menu items, otherwise false.
578 function wp_get_nav_menu_items( $menu, $args = array() ) {
579 $menu = wp_get_nav_menu_object( $menu );
585 static $fetched = array();
587 $items = get_objects_in_term( $menu->term_id, 'nav_menu' );
588 if ( is_wp_error( $items ) ) {
592 $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',
593 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true );
594 $args = wp_parse_args( $args, $defaults );
595 $args['include'] = $items;
597 if ( ! empty( $items ) ) {
598 $items = get_posts( $args );
603 // Get all posts and terms at once to prime the caches
604 if ( empty( $fetched[$menu->term_id] ) || wp_using_ext_object_cache() ) {
605 $fetched[$menu->term_id] = true;
608 foreach ( $items as $item ) {
609 $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
610 $object = get_post_meta( $item->ID, '_menu_item_object', true );
611 $type = get_post_meta( $item->ID, '_menu_item_type', true );
613 if ( 'post_type' == $type )
614 $posts[$object][] = $object_id;
615 elseif ( 'taxonomy' == $type)
616 $terms[$object][] = $object_id;
619 if ( ! empty( $posts ) ) {
620 foreach ( array_keys($posts) as $post_type ) {
621 get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) );
626 if ( ! empty( $terms ) ) {
627 foreach ( array_keys($terms) as $taxonomy ) {
628 get_terms( $taxonomy, array(
629 'include' => $terms[ $taxonomy ],
630 'hierarchical' => false,
637 $items = array_map( 'wp_setup_nav_menu_item', $items );
639 if ( ! is_admin() ) { // Remove invalid items only in frontend
640 $items = array_filter( $items, '_is_valid_nav_menu_item' );
643 if ( ARRAY_A == $args['output'] ) {
644 $GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
645 usort($items, '_sort_nav_menu_items');
647 foreach( $items as $k => $item ) {
648 $items[$k]->{$args['output_key']} = $i++;
653 * Filter the navigation menu items being returned.
657 * @param array $items An array of menu item post objects.
658 * @param object $menu The menu object.
659 * @param array $args An array of arguments used to retrieve menu item objects.
661 return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
665 * Decorates a menu item object with the shared navigation menu item properties.
668 * - ID: The term_id if the menu item represents a taxonomy term.
669 * - attr_title: The title attribute of the link element for this menu item.
670 * - classes: The array of class attribute values for the link element of this menu item.
671 * - db_id: The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
672 * - description: The description of this menu item.
673 * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
674 * - object: The type of object originally represented, such as "category," "post", or "attachment."
675 * - object_id: The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
676 * - post_parent: The DB ID of the original object's parent object, if any (0 otherwise).
677 * - post_title: A "no title" label if menu item represents a post that lacks a title.
678 * - target: The target attribute of the link element for this menu item.
679 * - title: The title of this menu item.
680 * - type: The family of objects originally represented, such as "post_type" or "taxonomy."
681 * - type_label: The singular label used to describe this type of menu item.
682 * - url: The URL to which this menu item points.
683 * - xfn: The XFN relationship expressed in the link of this menu item.
684 * - _invalid: Whether the menu item represents an object that no longer exists.
688 * @param object $menu_item The menu item to modify.
689 * @return object $menu_item The menu item with standard menu item properties.
691 function wp_setup_nav_menu_item( $menu_item ) {
692 if ( isset( $menu_item->post_type ) ) {
693 if ( 'nav_menu_item' == $menu_item->post_type ) {
694 $menu_item->db_id = (int) $menu_item->ID;
695 $menu_item->menu_item_parent = ! isset( $menu_item->menu_item_parent ) ? get_post_meta( $menu_item->ID, '_menu_item_menu_item_parent', true ) : $menu_item->menu_item_parent;
696 $menu_item->object_id = ! isset( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
697 $menu_item->object = ! isset( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
698 $menu_item->type = ! isset( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;
700 if ( 'post_type' == $menu_item->type ) {
701 $object = get_post_type_object( $menu_item->object );
703 $menu_item->type_label = $object->labels->singular_name;
705 $menu_item->type_label = $menu_item->object;
706 $menu_item->_invalid = true;
709 $menu_item->url = get_permalink( $menu_item->object_id );
711 $original_object = get_post( $menu_item->object_id );
712 $original_title = $original_object->post_title;
714 if ( '' === $original_title ) {
715 /* translators: %d: ID of a post */
716 $original_title = sprintf( __( '#%d (no title)' ), $original_object->ID );
719 $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
721 } elseif ( 'taxonomy' == $menu_item->type ) {
722 $object = get_taxonomy( $menu_item->object );
724 $menu_item->type_label = $object->labels->singular_name;
726 $menu_item->type_label = $menu_item->object;
727 $menu_item->_invalid = true;
730 $term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
731 $menu_item->url = !is_wp_error( $term_url ) ? $term_url : '';
733 $original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' );
734 if ( is_wp_error( $original_title ) )
735 $original_title = false;
736 $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
739 $menu_item->type_label = __('Custom Link');
740 $menu_item->title = $menu_item->post_title;
741 $menu_item->url = ! isset( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;
744 $menu_item->target = ! isset( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;
747 * Filter a navigation menu item's title attribute.
751 * @param string $item_title The menu item title attribute.
753 $menu_item->attr_title = ! isset( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;
755 if ( ! isset( $menu_item->description ) ) {
757 * Filter a navigation menu item's description.
761 * @param string $description The menu item description.
763 $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
766 $menu_item->classes = ! isset( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
767 $menu_item->xfn = ! isset( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
769 $menu_item->db_id = 0;
770 $menu_item->menu_item_parent = 0;
771 $menu_item->object_id = (int) $menu_item->ID;
772 $menu_item->type = 'post_type';
774 $object = get_post_type_object( $menu_item->post_type );
775 $menu_item->object = $object->name;
776 $menu_item->type_label = $object->labels->singular_name;
778 if ( '' === $menu_item->post_title ) {
779 /* translators: %d: ID of a post */
780 $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
783 $menu_item->title = $menu_item->post_title;
784 $menu_item->url = get_permalink( $menu_item->ID );
785 $menu_item->target = '';
787 /** This filter is documented in wp-includes/nav-menu.php */
788 $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );
790 /** This filter is documented in wp-includes/nav-menu.php */
791 $menu_item->description = apply_filters( 'nav_menu_description', '' );
792 $menu_item->classes = array();
793 $menu_item->xfn = '';
795 } elseif ( isset( $menu_item->taxonomy ) ) {
796 $menu_item->ID = $menu_item->term_id;
797 $menu_item->db_id = 0;
798 $menu_item->menu_item_parent = 0;
799 $menu_item->object_id = (int) $menu_item->term_id;
800 $menu_item->post_parent = (int) $menu_item->parent;
801 $menu_item->type = 'taxonomy';
803 $object = get_taxonomy( $menu_item->taxonomy );
804 $menu_item->object = $object->name;
805 $menu_item->type_label = $object->labels->singular_name;
807 $menu_item->title = $menu_item->name;
808 $menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy );
809 $menu_item->target = '';
810 $menu_item->attr_title = '';
811 $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );
812 $menu_item->classes = array();
813 $menu_item->xfn = '';
818 * Filter a navigation menu item object.
822 * @param object $menu_item The menu item object.
824 return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
828 * Get the menu items associated with a particular object.
832 * @param int $object_id The ID of the original object.
833 * @param string $object_type The type of object, such as "taxonomy" or "post_type."
834 * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
835 * @return array The array of menu item IDs; empty array if none;
837 function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
838 $object_id = (int) $object_id;
839 $menu_item_ids = array();
841 $query = new WP_Query;
842 $menu_items = $query->query(
844 'meta_key' => '_menu_item_object_id',
845 'meta_value' => $object_id,
846 'post_status' => 'any',
847 'post_type' => 'nav_menu_item',
848 'posts_per_page' => -1,
851 foreach( (array) $menu_items as $menu_item ) {
852 if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
853 $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
855 'post_type' == $object_type &&
856 'post_type' == $menu_item_type
858 $menu_item_ids[] = (int) $menu_item->ID;
860 'taxonomy' == $object_type &&
861 'taxonomy' == $menu_item_type &&
862 get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy
864 $menu_item_ids[] = (int) $menu_item->ID;
869 return array_unique( $menu_item_ids );
873 * Callback for handling a menu item when its original object is deleted.
878 * @param int $object_id The ID of the original object being trashed.
881 function _wp_delete_post_menu_item( $object_id = 0 ) {
882 $object_id = (int) $object_id;
884 $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
886 foreach( (array) $menu_item_ids as $menu_item_id ) {
887 wp_delete_post( $menu_item_id, true );
892 * Callback for handling a menu item when its original object is deleted.
897 * @param int $object_id The ID of the original object being trashed.
900 function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) {
901 $object_id = (int) $object_id;
903 $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
905 foreach( (array) $menu_item_ids as $menu_item_id ) {
906 wp_delete_post( $menu_item_id, true );
911 * Automatically add newly published page objects to menus with that as an option.
916 * @param string $new_status The new status of the post object.
917 * @param string $old_status The old status of the post object.
918 * @param object $post The post object being transitioned from one status to another.
920 function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
921 if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
923 if ( ! empty( $post->post_parent ) )
925 $auto_add = get_option( 'nav_menu_options' );
926 if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
928 $auto_add = $auto_add['auto_add'];
929 if ( empty( $auto_add ) || ! is_array( $auto_add ) )
933 'menu-item-object-id' => $post->ID,
934 'menu-item-object' => $post->post_type,
935 'menu-item-type' => 'post_type',
936 'menu-item-status' => 'publish',
939 foreach ( $auto_add as $menu_id ) {
940 $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
941 if ( ! is_array( $items ) )
943 foreach ( $items as $item ) {
944 if ( $post->ID == $item->object_id )
947 wp_update_nav_menu_item( $menu_id, 0, $args );