3 * Navigation Menu functions
6 * @subpackage Nav_Menus
11 * Returns a navigation menu object.
15 * @param string $menu Menu ID, slug, or name.
16 * @return mixed false if $menu param isn't supplied or term does not exist, menu object if successful.
18 function wp_get_nav_menu_object( $menu ) {
22 $menu_obj = get_term( $menu, 'nav_menu' );
25 $menu_obj = get_term_by( 'slug', $menu, 'nav_menu' );
28 $menu_obj = get_term_by( 'name', $menu, 'nav_menu' );
37 * Check if the given ID is a navigation menu.
39 * Returns true if it is; false otherwise.
43 * @param int|string $menu The menu to check (ID, slug, or name).
44 * @return bool Whether the menu exists.
46 function is_nav_menu( $menu ) {
50 $menu_obj = wp_get_nav_menu_object( $menu );
54 ! is_wp_error( $menu_obj ) &&
55 ! empty( $menu_obj->taxonomy ) &&
56 'nav_menu' == $menu_obj->taxonomy
64 * Register navigation menus for a theme.
68 * @param array $locations Associative array of menu location identifiers (like a slug) and descriptive text.
70 function register_nav_menus( $locations = array() ) {
71 global $_wp_registered_nav_menus;
73 add_theme_support( 'menus' );
75 $_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations );
79 * Unregisters a navigation menu for a theme.
81 * @param string $location The menu location identifier.
82 * @return bool True on success, false on failure.
84 function unregister_nav_menu( $location ) {
85 global $_wp_registered_nav_menus;
87 if ( is_array( $_wp_registered_nav_menus ) && isset( $_wp_registered_nav_menus[$location] ) ) {
88 unset( $_wp_registered_nav_menus[$location] );
89 if ( empty( $_wp_registered_nav_menus ) ) {
90 _remove_theme_support( 'menus' );
98 * Register a navigation menu for a theme.
102 * @param string $location Menu location identifier, like a slug.
103 * @param string $description Menu location descriptive text.
105 function register_nav_menu( $location, $description ) {
106 register_nav_menus( array( $location => $description ) );
109 * Returns an array of all registered navigation menus in a theme
114 function get_registered_nav_menus() {
115 global $_wp_registered_nav_menus;
116 if ( isset( $_wp_registered_nav_menus ) )
117 return $_wp_registered_nav_menus;
122 * Returns an array with the registered navigation menu locations and the menu assigned to it
128 function get_nav_menu_locations() {
129 $locations = get_theme_mod( 'nav_menu_locations' );
130 return ( is_array( $locations ) ) ? $locations : array();
134 * Whether a registered nav menu location has a menu assigned to it.
137 * @param string $location Menu location identifier.
138 * @return bool Whether location has a menu.
140 function has_nav_menu( $location ) {
141 $registered_nav_menus = get_registered_nav_menus();
142 if ( ! isset( $registered_nav_menus[ $location ] ) ) {
146 $locations = get_nav_menu_locations();
147 return ( ! empty( $locations[ $location ] ) );
151 * Determine whether the given ID is a nav menu item.
155 * @param int $menu_item_id The ID of the potential nav menu item.
156 * @return bool Whether the given ID is that of a nav menu item.
158 function is_nav_menu_item( $menu_item_id = 0 ) {
159 return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' == get_post_type( $menu_item_id ) ) );
163 * Create a Navigation Menu.
167 * @param string $menu_name Menu name.
168 * @return int|WP_Error Menu ID on success, WP_Error object on failure.
170 function wp_create_nav_menu( $menu_name ) {
171 return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
175 * Delete a Navigation Menu.
179 * @param string $menu Menu ID, slug, or name.
180 * @return bool|WP_Error True on success, false or WP_Error object on failure.
182 function wp_delete_nav_menu( $menu ) {
183 $menu = wp_get_nav_menu_object( $menu );
187 $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
188 if ( ! empty( $menu_objects ) ) {
189 foreach ( $menu_objects as $item ) {
190 wp_delete_post( $item );
194 $result = wp_delete_term( $menu->term_id, 'nav_menu' );
196 // Remove this menu from any locations.
197 $locations = get_nav_menu_locations();
198 foreach ( $locations as $location => $menu_id ) {
199 if ( $menu_id == $menu->term_id )
200 $locations[ $location ] = 0;
202 set_theme_mod( 'nav_menu_locations', $locations );
204 if ( $result && !is_wp_error($result) )
207 * Fires after a navigation menu has been successfully deleted.
211 * @param int $term_id ID of the deleted menu.
213 do_action( 'wp_delete_nav_menu', $menu->term_id );
219 * Save the properties of a menu or create a new menu with those properties.
223 * @param int $menu_id The ID of the menu or "0" to create a new menu.
224 * @param array $menu_data The array of menu data.
225 * @return int|WP_Error Menu ID on success, WP_Error object on failure.
227 function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
228 $menu_id = (int) $menu_id;
230 $_menu = wp_get_nav_menu_object( $menu_id );
233 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),
234 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),
235 'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),
239 // double-check that we're not going to have one menu take the name of another
240 $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
242 $_possible_existing &&
243 ! is_wp_error( $_possible_existing ) &&
244 isset( $_possible_existing->term_id ) &&
245 $_possible_existing->term_id != $menu_id
247 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'] ) ) );
249 // menu doesn't already exist, so create a new menu
250 if ( ! $_menu || is_wp_error( $_menu ) ) {
251 $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
254 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'] ) ) );
256 $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );
258 if ( is_wp_error( $_menu ) )
262 * Fires after a navigation menu is successfully created.
266 * @param int $term_id ID of the new menu.
267 * @param array $menu_data An array of menu data.
269 do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
271 return (int) $_menu['term_id'];
274 if ( ! $_menu || ! isset( $_menu->term_id ) )
277 $menu_id = (int) $_menu->term_id;
279 $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
281 if ( is_wp_error( $update_response ) )
282 return $update_response;
285 * Fires after a navigation menu has been successfully updated.
289 * @param int $menu_id ID of the updated menu.
290 * @param array $menu_data An array of menu data.
292 do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
297 * Save the properties of a menu item or create a new one.
301 * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
302 * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
303 * @param array $menu_item_data The menu item's data.
304 * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
306 function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) {
307 $menu_id = (int) $menu_id;
308 $menu_item_db_id = (int) $menu_item_db_id;
310 // make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects
311 if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) )
312 return new WP_Error( 'update_nav_menu_item_failed', __( 'The given object ID is not that of a menu item.' ) );
314 $menu = wp_get_nav_menu_object( $menu_id );
316 if ( ! $menu && 0 !== $menu_id ) {
317 return new WP_Error( 'invalid_menu_id', __( 'Invalid menu ID.' ) );
320 if ( is_wp_error( $menu ) ) {
325 'menu-item-db-id' => $menu_item_db_id,
326 'menu-item-object-id' => 0,
327 'menu-item-object' => '',
328 'menu-item-parent-id' => 0,
329 'menu-item-position' => 0,
330 'menu-item-type' => 'custom',
331 'menu-item-title' => '',
332 'menu-item-url' => '',
333 'menu-item-description' => '',
334 'menu-item-attr-title' => '',
335 'menu-item-target' => '',
336 'menu-item-classes' => '',
337 'menu-item-xfn' => '',
338 'menu-item-status' => '',
341 $args = wp_parse_args( $menu_item_data, $defaults );
343 if ( 0 == $menu_id ) {
344 $args['menu-item-position'] = 1;
345 } elseif ( 0 == (int) $args['menu-item-position'] ) {
346 $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
347 $last_item = array_pop( $menu_items );
348 $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items );
351 $original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;
353 if ( 'custom' != $args['menu-item-type'] ) {
354 /* if non-custom menu item, then:
355 * use original object's URL
356 * blank default title to sync with original object's
359 $args['menu-item-url'] = '';
361 $original_title = '';
362 if ( 'taxonomy' == $args['menu-item-type'] ) {
363 $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
364 $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
365 } elseif ( 'post_type' == $args['menu-item-type'] ) {
367 $original_object = get_post( $args['menu-item-object-id'] );
368 $original_parent = (int) $original_object->post_parent;
369 $original_title = $original_object->post_title;
372 if ( $args['menu-item-title'] == $original_title )
373 $args['menu-item-title'] = '';
375 // hack to get wp to create a post object when too many properties are empty
376 if ( '' == $args['menu-item-title'] && '' == $args['menu-item-description'] )
377 $args['menu-item-description'] = ' ';
380 // Populate the menu item object
382 'menu_order' => $args['menu-item-position'],
384 'post_content' => $args['menu-item-description'],
385 'post_excerpt' => $args['menu-item-attr-title'],
386 'post_parent' => $original_parent,
387 'post_title' => $args['menu-item-title'],
388 'post_type' => 'nav_menu_item',
391 $update = 0 != $menu_item_db_id;
393 // New menu item. Default is draft status
396 $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
397 $menu_item_db_id = wp_insert_post( $post );
398 if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
399 return $menu_item_db_id;
402 // Associate the menu item with the menu term
403 // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
404 if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
405 wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
408 if ( 'custom' == $args['menu-item-type'] ) {
409 $args['menu-item-object-id'] = $menu_item_db_id;
410 $args['menu-item-object'] = 'custom';
413 $menu_item_db_id = (int) $menu_item_db_id;
415 update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']) );
416 update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', strval( (int) $args['menu-item-parent-id'] ) );
417 update_post_meta( $menu_item_db_id, '_menu_item_object_id', strval( (int) $args['menu-item-object-id'] ) );
418 update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']) );
419 update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']) );
421 $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) );
422 $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );
423 update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
424 update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
425 update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) );
428 update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
429 elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) )
430 delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
432 // Update existing menu item. Default is publish status
434 $post['ID'] = $menu_item_db_id;
435 $post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
436 wp_update_post( $post );
440 * Fires after a navigation menu item has been updated.
444 * @see wp_update_nav_menu_item()
446 * @param int $menu_id ID of the updated menu.
447 * @param int $menu_item_db_id ID of the updated menu item.
448 * @param array $args An array of arguments used to update a menu item.
450 do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
452 return $menu_item_db_id;
456 * Returns all navigation menu objects.
459 * @since 4.1.0 Default value of the 'orderby' argument was changed from 'none'
462 * @param array $args Optional. Array of arguments passed on to {@see get_terms()}.
463 * Default empty array.
464 * @return array Menu objects.
466 function wp_get_nav_menus( $args = array() ) {
467 $defaults = array( 'hide_empty' => false, 'orderby' => 'name' );
468 $args = wp_parse_args( $args, $defaults );
471 * Filter the navigation menu objects being returned.
477 * @param array $menus An array of menu objects.
478 * @param array $args An array of arguments used to retrieve menu objects.
480 return apply_filters( 'wp_get_nav_menus', get_terms( 'nav_menu', $args), $args );
484 * Sort menu items by the desired key.
489 * @param object $a The first object to compare
490 * @param object $b The second object to compare
491 * @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b.
493 function _sort_nav_menu_items( $a, $b ) {
494 global $_menu_item_sort_prop;
496 if ( empty( $_menu_item_sort_prop ) )
499 if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) )
502 $_a = (int) $a->$_menu_item_sort_prop;
503 $_b = (int) $b->$_menu_item_sort_prop;
505 if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop )
507 elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop )
508 return $_a < $_b ? -1 : 1;
510 return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop );
514 * Return if a menu item is valid.
516 * @link https://core.trac.wordpress.org/ticket/13958
521 * @param object $item The menu item to check.
522 * @return bool False if invalid, otherwise true.
524 function _is_valid_nav_menu_item( $item ) {
525 if ( ! empty( $item->_invalid ) )
532 * Return all menu items of a navigation menu.
536 * @param string $menu Menu name, ID, or slug.
537 * @param array $args Optional. Arguments to pass to {@see get_posts()}.
538 * @return mixed $items Array of menu items, otherwise false.
540 function wp_get_nav_menu_items( $menu, $args = array() ) {
541 $menu = wp_get_nav_menu_object( $menu );
546 static $fetched = array();
548 $items = get_objects_in_term( $menu->term_id, 'nav_menu' );
550 if ( empty( $items ) )
553 $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',
554 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true );
555 $args = wp_parse_args( $args, $defaults );
556 $args['include'] = $items;
558 $items = get_posts( $args );
560 if ( is_wp_error( $items ) || ! is_array( $items ) )
563 // Get all posts and terms at once to prime the caches
564 if ( empty( $fetched[$menu->term_id] ) || wp_using_ext_object_cache() ) {
565 $fetched[$menu->term_id] = true;
568 foreach ( $items as $item ) {
569 $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
570 $object = get_post_meta( $item->ID, '_menu_item_object', true );
571 $type = get_post_meta( $item->ID, '_menu_item_type', true );
573 if ( 'post_type' == $type )
574 $posts[$object][] = $object_id;
575 elseif ( 'taxonomy' == $type)
576 $terms[$object][] = $object_id;
579 if ( ! empty( $posts ) ) {
580 foreach ( array_keys($posts) as $post_type ) {
581 get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) );
586 if ( ! empty( $terms ) ) {
587 foreach ( array_keys($terms) as $taxonomy ) {
588 get_terms($taxonomy, array('include' => $terms[$taxonomy]) );
594 $items = array_map( 'wp_setup_nav_menu_item', $items );
596 if ( ! is_admin() ) // Remove invalid items only in frontend
597 $items = array_filter( $items, '_is_valid_nav_menu_item' );
599 if ( ARRAY_A == $args['output'] ) {
600 $GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
601 usort($items, '_sort_nav_menu_items');
603 foreach( $items as $k => $item ) {
604 $items[$k]->$args['output_key'] = $i++;
609 * Filter the navigation menu items being returned.
613 * @param array $items An array of menu item post objects.
614 * @param object $menu The menu object.
615 * @param array $args An array of arguments used to retrieve menu item objects.
617 return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
621 * Decorates a menu item object with the shared navigation menu item properties.
624 * - db_id: The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
625 * - object_id: The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
626 * - type: The family of objects originally represented, such as "post_type" or "taxonomy."
627 * - object: The type of object originally represented, such as "category," "post", or "attachment."
628 * - type_label: The singular label used to describe this type of menu item.
629 * - post_parent: The DB ID of the original object's parent object, if any (0 otherwise).
630 * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
631 * - url: The URL to which this menu item points.
632 * - title: The title of this menu item.
633 * - target: The target attribute of the link element for this menu item.
634 * - attr_title: The title attribute of the link element for this menu item.
635 * - classes: The array of class attribute values for the link element of this menu item.
636 * - xfn: The XFN relationship expressed in the link of this menu item.
637 * - description: The description of this menu item.
641 * @param object $menu_item The menu item to modify.
642 * @return object $menu_item The menu item with standard menu item properties.
644 function wp_setup_nav_menu_item( $menu_item ) {
645 if ( isset( $menu_item->post_type ) ) {
646 if ( 'nav_menu_item' == $menu_item->post_type ) {
647 $menu_item->db_id = (int) $menu_item->ID;
648 $menu_item->menu_item_parent = empty( $menu_item->menu_item_parent ) ? get_post_meta( $menu_item->ID, '_menu_item_menu_item_parent', true ) : $menu_item->menu_item_parent;
649 $menu_item->object_id = empty( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
650 $menu_item->object = empty( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
651 $menu_item->type = empty( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;
653 if ( 'post_type' == $menu_item->type ) {
654 $object = get_post_type_object( $menu_item->object );
656 $menu_item->type_label = $object->labels->singular_name;
658 $menu_item->type_label = $menu_item->object;
659 $menu_item->_invalid = true;
662 $menu_item->url = get_permalink( $menu_item->object_id );
664 $original_object = get_post( $menu_item->object_id );
665 $original_title = $original_object->post_title;
667 if ( '' === $original_title ) {
668 $original_title = sprintf( __( '#%d (no title)' ), $original_object->ID );
671 $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
673 } elseif ( 'taxonomy' == $menu_item->type ) {
674 $object = get_taxonomy( $menu_item->object );
676 $menu_item->type_label = $object->labels->singular_name;
678 $menu_item->type_label = $menu_item->object;
679 $menu_item->_invalid = true;
682 $term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
683 $menu_item->url = !is_wp_error( $term_url ) ? $term_url : '';
685 $original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' );
686 if ( is_wp_error( $original_title ) )
687 $original_title = false;
688 $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
691 $menu_item->type_label = __('Custom Link');
692 $menu_item->title = $menu_item->post_title;
693 $menu_item->url = empty( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;
696 $menu_item->target = empty( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;
699 * Filter a navigation menu item's title attribute.
703 * @param string $item_title The menu item title attribute.
705 $menu_item->attr_title = empty( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;
707 if ( empty( $menu_item->description ) ) {
709 * Filter a navigation menu item's description.
713 * @param string $description The menu item description.
715 $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
718 $menu_item->classes = empty( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
719 $menu_item->xfn = empty( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
721 $menu_item->db_id = 0;
722 $menu_item->menu_item_parent = 0;
723 $menu_item->object_id = (int) $menu_item->ID;
724 $menu_item->type = 'post_type';
726 $object = get_post_type_object( $menu_item->post_type );
727 $menu_item->object = $object->name;
728 $menu_item->type_label = $object->labels->singular_name;
730 if ( '' === $menu_item->post_title )
731 $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
733 $menu_item->title = $menu_item->post_title;
734 $menu_item->url = get_permalink( $menu_item->ID );
735 $menu_item->target = '';
737 /** This filter is documented in wp-includes/nav-menu.php */
738 $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );
740 /** This filter is documented in wp-includes/nav-menu.php */
741 $menu_item->description = apply_filters( 'nav_menu_description', '' );
742 $menu_item->classes = array();
743 $menu_item->xfn = '';
745 } elseif ( isset( $menu_item->taxonomy ) ) {
746 $menu_item->ID = $menu_item->term_id;
747 $menu_item->db_id = 0;
748 $menu_item->menu_item_parent = 0;
749 $menu_item->object_id = (int) $menu_item->term_id;
750 $menu_item->post_parent = (int) $menu_item->parent;
751 $menu_item->type = 'taxonomy';
753 $object = get_taxonomy( $menu_item->taxonomy );
754 $menu_item->object = $object->name;
755 $menu_item->type_label = $object->labels->singular_name;
757 $menu_item->title = $menu_item->name;
758 $menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy );
759 $menu_item->target = '';
760 $menu_item->attr_title = '';
761 $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );
762 $menu_item->classes = array();
763 $menu_item->xfn = '';
768 * Filter a navigation menu item object.
772 * @param object $menu_item The menu item object.
774 return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
778 * Get the menu items associated with a particular object.
782 * @param int $object_id The ID of the original object.
783 * @param string $object_type The type of object, such as "taxonomy" or "post_type."
784 * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
785 * @return array The array of menu item IDs; empty array if none;
787 function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
788 $object_id = (int) $object_id;
789 $menu_item_ids = array();
791 $query = new WP_Query;
792 $menu_items = $query->query(
794 'meta_key' => '_menu_item_object_id',
795 'meta_value' => $object_id,
796 'post_status' => 'any',
797 'post_type' => 'nav_menu_item',
798 'posts_per_page' => -1,
801 foreach( (array) $menu_items as $menu_item ) {
802 if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
803 $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
805 'post_type' == $object_type &&
806 'post_type' == $menu_item_type
808 $menu_item_ids[] = (int) $menu_item->ID;
810 'taxonomy' == $object_type &&
811 'taxonomy' == $menu_item_type &&
812 get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy
814 $menu_item_ids[] = (int) $menu_item->ID;
819 return array_unique( $menu_item_ids );
823 * Callback for handling a menu item when its original object is deleted.
828 * @param int $object_id The ID of the original object being trashed.
831 function _wp_delete_post_menu_item( $object_id = 0 ) {
832 $object_id = (int) $object_id;
834 $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
836 foreach( (array) $menu_item_ids as $menu_item_id ) {
837 wp_delete_post( $menu_item_id, true );
842 * Callback for handling a menu item when its original object is deleted.
847 * @param int $object_id The ID of the original object being trashed.
850 function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) {
851 $object_id = (int) $object_id;
853 $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
855 foreach( (array) $menu_item_ids as $menu_item_id ) {
856 wp_delete_post( $menu_item_id, true );
861 * Automatically add newly published page objects to menus with that as an option.
866 * @param string $new_status The new status of the post object.
867 * @param string $old_status The old status of the post object.
868 * @param object $post The post object being transitioned from one status to another.
871 function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
872 if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
874 if ( ! empty( $post->post_parent ) )
876 $auto_add = get_option( 'nav_menu_options' );
877 if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
879 $auto_add = $auto_add['auto_add'];
880 if ( empty( $auto_add ) || ! is_array( $auto_add ) )
884 'menu-item-object-id' => $post->ID,
885 'menu-item-object' => $post->post_type,
886 'menu-item-type' => 'post_type',
887 'menu-item-status' => 'publish',
890 foreach ( $auto_add as $menu_id ) {
891 $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
892 if ( ! is_array( $items ) )
894 foreach ( $items as $item ) {
895 if ( $post->ID == $item->object_id )
898 wp_update_nav_menu_item( $menu_id, 0, $args );