3 * Navigation Menu functions
6 * @subpackage Nav_Menus
11 * Returns a navigation menu object.
18 * @param string $menu Menu ID, slug, or name.
19 * @return mixed false if $menu param isn't supplied or term does not exist, menu object if successful.
21 function wp_get_nav_menu_object( $menu ) {
25 $menu_obj = get_term( $menu, 'nav_menu' );
28 $menu_obj = get_term_by( 'slug', $menu, 'nav_menu' );
31 $menu_obj = get_term_by( 'name', $menu, 'nav_menu' );
40 * Check if the given ID is a navigation menu.
42 * Returns true if it is; false otherwise.
46 * @param int|string $menu The menu to check (ID, slug, or name).
47 * @return bool Whether the menu exists.
49 function is_nav_menu( $menu ) {
53 $menu_obj = wp_get_nav_menu_object( $menu );
57 ! is_wp_error( $menu_obj ) &&
58 ! empty( $menu_obj->taxonomy ) &&
59 'nav_menu' == $menu_obj->taxonomy
67 * Register navigation menus for a theme.
71 * @param array $locations Associative array of menu location identifiers (like a slug) and descriptive text.
73 function register_nav_menus( $locations = array() ) {
74 global $_wp_registered_nav_menus;
76 add_theme_support( 'menus' );
78 $_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations );
82 * Unregisters a navigation menu for a theme.
84 * @param array $location the menu location identifier
86 * @return bool True on success, false on failure.
88 function unregister_nav_menu( $location ) {
89 global $_wp_registered_nav_menus;
91 if ( is_array( $_wp_registered_nav_menus ) && isset( $_wp_registered_nav_menus[$location] ) ) {
92 unset( $_wp_registered_nav_menus[$location] );
93 if ( empty( $_wp_registered_nav_menus ) ) {
94 _remove_theme_support( 'menus' );
102 * Register a navigation menu for a theme.
106 * @param string $location Menu location identifier, like a slug.
107 * @param string $description Menu location descriptive text.
109 function register_nav_menu( $location, $description ) {
110 register_nav_menus( array( $location => $description ) );
113 * Returns an array of all registered navigation menus in a theme
118 function get_registered_nav_menus() {
119 global $_wp_registered_nav_menus;
120 if ( isset( $_wp_registered_nav_menus ) )
121 return $_wp_registered_nav_menus;
126 * Returns an array with the registered navigation menu locations and the menu assigned to it
132 function get_nav_menu_locations() {
133 $locations = get_theme_mod( 'nav_menu_locations' );
134 return ( is_array( $locations ) ) ? $locations : array();
138 * Whether a registered nav menu location has a menu assigned to it.
141 * @param string $location Menu location identifier.
142 * @return bool Whether location has a menu.
144 function has_nav_menu( $location ) {
145 global $_wp_registered_nav_menus;
147 if ( ! isset( $_wp_registered_nav_menus[ $location ] ) ) {
151 $locations = get_nav_menu_locations();
152 return ( ! empty( $locations[ $location ] ) );
156 * Determine whether the given ID is a nav menu item.
160 * @param int $menu_item_id The ID of the potential nav menu item.
161 * @return bool Whether the given ID is that of a nav menu item.
163 function is_nav_menu_item( $menu_item_id = 0 ) {
164 return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' == get_post_type( $menu_item_id ) ) );
168 * Create a Navigation Menu.
172 * @param string $menu_name Menu name.
173 * @return int|WP_Error Menu ID on success, WP_Error object on failure.
175 function wp_create_nav_menu( $menu_name ) {
176 return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
180 * Delete a Navigation Menu.
184 * @param string $menu Menu ID, slug, or name.
185 * @return bool|WP_Error True on success, false or WP_Error object on failure.
187 function wp_delete_nav_menu( $menu ) {
188 $menu = wp_get_nav_menu_object( $menu );
192 $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
193 if ( ! empty( $menu_objects ) ) {
194 foreach ( $menu_objects as $item ) {
195 wp_delete_post( $item );
199 $result = wp_delete_term( $menu->term_id, 'nav_menu' );
201 // Remove this menu from any locations.
202 $locations = get_nav_menu_locations();
203 foreach ( $locations as $location => $menu_id ) {
204 if ( $menu_id == $menu->term_id )
205 $locations[ $location ] = 0;
207 set_theme_mod( 'nav_menu_locations', $locations );
209 if ( $result && !is_wp_error($result) )
212 * Fires after a navigation menu has been successfully deleted.
216 * @param int $term_id ID of the deleted menu.
218 do_action( 'wp_delete_nav_menu', $menu->term_id );
224 * Save the properties of a menu or create a new menu with those properties.
228 * @param int $menu_id The ID of the menu or "0" to create a new menu.
229 * @param array $menu_data The array of menu data.
230 * @return int|WP_Error Menu ID on success, WP_Error object on failure.
232 function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
233 $menu_id = (int) $menu_id;
235 $_menu = wp_get_nav_menu_object( $menu_id );
238 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),
239 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),
240 'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),
244 // double-check that we're not going to have one menu take the name of another
245 $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
247 $_possible_existing &&
248 ! is_wp_error( $_possible_existing ) &&
249 isset( $_possible_existing->term_id ) &&
250 $_possible_existing->term_id != $menu_id
252 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'] ) ) );
254 // menu doesn't already exist, so create a new menu
255 if ( ! $_menu || is_wp_error( $_menu ) ) {
256 $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
259 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'] ) ) );
261 $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );
263 if ( is_wp_error( $_menu ) )
267 * Fires after a navigation menu is successfully created.
271 * @param int $term_id ID of the new menu.
272 * @param array $menu_data An array of menu data.
274 do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
276 return (int) $_menu['term_id'];
279 if ( ! $_menu || ! isset( $_menu->term_id ) )
282 $menu_id = (int) $_menu->term_id;
284 $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
286 if ( is_wp_error( $update_response ) )
287 return $update_response;
290 * Fires after a navigation menu has been successfully updated.
294 * @param int $menu_id ID of the updated menu.
295 * @param array $menu_data An array of menu data.
297 do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
302 * Save the properties of a menu item or create a new one.
306 * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
307 * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
308 * @param array $menu_item_data The menu item's data.
309 * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
311 function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) {
312 $menu_id = (int) $menu_id;
313 $menu_item_db_id = (int) $menu_item_db_id;
315 // make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects
316 if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) )
317 return new WP_Error( 'update_nav_menu_item_failed', __( 'The given object ID is not that of a menu item.' ) );
319 $menu = wp_get_nav_menu_object( $menu_id );
321 if ( ! $menu && 0 !== $menu_id ) {
322 return new WP_Error( 'invalid_menu_id', __( 'Invalid menu ID.' ) );
325 if ( is_wp_error( $menu ) ) {
330 'menu-item-db-id' => $menu_item_db_id,
331 'menu-item-object-id' => 0,
332 'menu-item-object' => '',
333 'menu-item-parent-id' => 0,
334 'menu-item-position' => 0,
335 'menu-item-type' => 'custom',
336 'menu-item-title' => '',
337 'menu-item-url' => '',
338 'menu-item-description' => '',
339 'menu-item-attr-title' => '',
340 'menu-item-target' => '',
341 'menu-item-classes' => '',
342 'menu-item-xfn' => '',
343 'menu-item-status' => '',
346 $args = wp_parse_args( $menu_item_data, $defaults );
348 if ( 0 == $menu_id ) {
349 $args['menu-item-position'] = 1;
350 } elseif ( 0 == (int) $args['menu-item-position'] ) {
351 $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
352 $last_item = array_pop( $menu_items );
353 $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items );
356 $original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;
358 if ( 'custom' != $args['menu-item-type'] ) {
359 /* if non-custom menu item, then:
360 * use original object's URL
361 * blank default title to sync with original object's
364 $args['menu-item-url'] = '';
366 $original_title = '';
367 if ( 'taxonomy' == $args['menu-item-type'] ) {
368 $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
369 $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
370 } elseif ( 'post_type' == $args['menu-item-type'] ) {
372 $original_object = get_post( $args['menu-item-object-id'] );
373 $original_parent = (int) $original_object->post_parent;
374 $original_title = $original_object->post_title;
377 if ( $args['menu-item-title'] == $original_title )
378 $args['menu-item-title'] = '';
380 // hack to get wp to create a post object when too many properties are empty
381 if ( '' == $args['menu-item-title'] && '' == $args['menu-item-description'] )
382 $args['menu-item-description'] = ' ';
385 // Populate the menu item object
387 'menu_order' => $args['menu-item-position'],
389 'post_content' => $args['menu-item-description'],
390 'post_excerpt' => $args['menu-item-attr-title'],
391 'post_parent' => $original_parent,
392 'post_title' => $args['menu-item-title'],
393 'post_type' => 'nav_menu_item',
396 $update = 0 != $menu_item_db_id;
398 // New menu item. Default is draft status
401 $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
402 $menu_item_db_id = wp_insert_post( $post );
403 if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
404 return $menu_item_db_id;
407 // Associate the menu item with the menu term
408 // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
409 if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
410 wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
413 if ( 'custom' == $args['menu-item-type'] ) {
414 $args['menu-item-object-id'] = $menu_item_db_id;
415 $args['menu-item-object'] = 'custom';
418 $menu_item_db_id = (int) $menu_item_db_id;
420 update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']) );
421 update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', strval( (int) $args['menu-item-parent-id'] ) );
422 update_post_meta( $menu_item_db_id, '_menu_item_object_id', strval( (int) $args['menu-item-object-id'] ) );
423 update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']) );
424 update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']) );
426 $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) );
427 $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );
428 update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
429 update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
430 update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) );
433 update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
434 elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) )
435 delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
437 // Update existing menu item. Default is publish status
439 $post['ID'] = $menu_item_db_id;
440 $post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
441 wp_update_post( $post );
445 * Fires after a navigation menu item has been updated.
449 * @see wp_update_nav_menu_items()
451 * @param int $menu_id ID of the updated menu.
452 * @param int $menu_item_db_id ID of the updated menu item.
453 * @param array $args An array of arguments used to update a menu item.
455 do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
457 return $menu_item_db_id;
461 * Returns all navigation menu objects.
465 * @param array $args Array of arguments passed on to get_terms().
466 * @return array menu objects
468 function wp_get_nav_menus( $args = array() ) {
469 $defaults = array( 'hide_empty' => false, 'orderby' => 'none' );
470 $args = wp_parse_args( $args, $defaults );
473 * Filter the navigation menu objects being returned.
479 * @param array $menus An array of menu objects.
480 * @param array $args An array of arguments used to retrieve menu objects.
482 return apply_filters( 'wp_get_nav_menus', get_terms( 'nav_menu', $args), $args );
486 * Sort menu items by the desired key.
491 * @param object $a The first object to compare
492 * @param object $b The second object to compare
493 * @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b.
495 function _sort_nav_menu_items( $a, $b ) {
496 global $_menu_item_sort_prop;
498 if ( empty( $_menu_item_sort_prop ) )
501 if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) )
504 $_a = (int) $a->$_menu_item_sort_prop;
505 $_b = (int) $b->$_menu_item_sort_prop;
507 if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop )
509 elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop )
510 return $_a < $_b ? -1 : 1;
512 return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop );
516 * Returns if a menu item is valid. Bug #13958
521 * @param object $menu_item The menu item to check
522 * @return bool false if invalid, else true.
524 function _is_valid_nav_menu_item( $item ) {
525 if ( ! empty( $item->_invalid ) )
532 * Returns all menu items of a navigation menu.
536 * @param string $menu menu name, id, or slug
537 * @param string $args
538 * @return mixed $items array of menu items, else 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;
666 $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
668 } elseif ( 'taxonomy' == $menu_item->type ) {
669 $object = get_taxonomy( $menu_item->object );
671 $menu_item->type_label = $object->labels->singular_name;
673 $menu_item->type_label = $menu_item->object;
674 $menu_item->_invalid = true;
677 $term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
678 $menu_item->url = !is_wp_error( $term_url ) ? $term_url : '';
680 $original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' );
681 if ( is_wp_error( $original_title ) )
682 $original_title = false;
683 $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
686 $menu_item->type_label = __('Custom');
687 $menu_item->title = $menu_item->post_title;
688 $menu_item->url = empty( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;
691 $menu_item->target = empty( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;
694 * Filter a navigation menu item's title attribute.
698 * @param string $item_title The menu item title attribute.
700 $menu_item->attr_title = empty( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;
702 if ( empty( $menu_item->description ) ) {
704 * Filter a navigation menu item's description.
708 * @param string $description The menu item description.
710 $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
713 $menu_item->classes = empty( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
714 $menu_item->xfn = empty( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
716 $menu_item->db_id = 0;
717 $menu_item->menu_item_parent = 0;
718 $menu_item->object_id = (int) $menu_item->ID;
719 $menu_item->type = 'post_type';
721 $object = get_post_type_object( $menu_item->post_type );
722 $menu_item->object = $object->name;
723 $menu_item->type_label = $object->labels->singular_name;
725 if ( '' === $menu_item->post_title )
726 $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
728 $menu_item->title = $menu_item->post_title;
729 $menu_item->url = get_permalink( $menu_item->ID );
730 $menu_item->target = '';
732 /** This filter is documented in wp-includes/nav-menu.php */
733 $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );
735 /** This filter is documented in wp-includes/nav-menu.php */
736 $menu_item->description = apply_filters( 'nav_menu_description', '' );
737 $menu_item->classes = array();
738 $menu_item->xfn = '';
740 } elseif ( isset( $menu_item->taxonomy ) ) {
741 $menu_item->ID = $menu_item->term_id;
742 $menu_item->db_id = 0;
743 $menu_item->menu_item_parent = 0;
744 $menu_item->object_id = (int) $menu_item->term_id;
745 $menu_item->post_parent = (int) $menu_item->parent;
746 $menu_item->type = 'taxonomy';
748 $object = get_taxonomy( $menu_item->taxonomy );
749 $menu_item->object = $object->name;
750 $menu_item->type_label = $object->labels->singular_name;
752 $menu_item->title = $menu_item->name;
753 $menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy );
754 $menu_item->target = '';
755 $menu_item->attr_title = '';
756 $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );
757 $menu_item->classes = array();
758 $menu_item->xfn = '';
763 * Filter a navigation menu item object.
767 * @param object $menu_item The menu item object.
769 return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
773 * Get the menu items associated with a particular object.
777 * @param int $object_id The ID of the original object.
778 * @param string $object_type The type of object, such as "taxonomy" or "post_type."
779 * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
780 * @return array The array of menu item IDs; empty array if none;
782 function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
783 $object_id = (int) $object_id;
784 $menu_item_ids = array();
786 $query = new WP_Query;
787 $menu_items = $query->query(
789 'meta_key' => '_menu_item_object_id',
790 'meta_value' => $object_id,
791 'post_status' => 'any',
792 'post_type' => 'nav_menu_item',
793 'posts_per_page' => -1,
796 foreach( (array) $menu_items as $menu_item ) {
797 if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
798 $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
800 'post_type' == $object_type &&
801 'post_type' == $menu_item_type
803 $menu_item_ids[] = (int) $menu_item->ID;
805 'taxonomy' == $object_type &&
806 'taxonomy' == $menu_item_type &&
807 get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy
809 $menu_item_ids[] = (int) $menu_item->ID;
814 return array_unique( $menu_item_ids );
818 * Callback for handling a menu item when its original object is deleted.
823 * @param int $object_id The ID of the original object being trashed.
826 function _wp_delete_post_menu_item( $object_id = 0 ) {
827 $object_id = (int) $object_id;
829 $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
831 foreach( (array) $menu_item_ids as $menu_item_id ) {
832 wp_delete_post( $menu_item_id, true );
837 * Callback for handling a menu item when its original object is deleted.
842 * @param int $object_id The ID of the original object being trashed.
845 function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) {
846 $object_id = (int) $object_id;
848 $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
850 foreach( (array) $menu_item_ids as $menu_item_id ) {
851 wp_delete_post( $menu_item_id, true );
856 * Automatically add newly published page objects to menus with that as an option.
861 * @param string $new_status The new status of the post object.
862 * @param string $old_status The old status of the post object.
863 * @param object $post The post object being transitioned from one status to another.
866 function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
867 if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
869 if ( ! empty( $post->post_parent ) )
871 $auto_add = get_option( 'nav_menu_options' );
872 if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
874 $auto_add = $auto_add['auto_add'];
875 if ( empty( $auto_add ) || ! is_array( $auto_add ) )
879 'menu-item-object-id' => $post->ID,
880 'menu-item-object' => $post->post_type,
881 'menu-item-type' => 'post_type',
882 'menu-item-status' => 'publish',
885 foreach ( $auto_add as $menu_id ) {
886 $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
887 if ( ! is_array( $items ) )
889 foreach ( $items as $item ) {
890 if ( $post->ID == $item->object_id )
893 wp_update_nav_menu_item( $menu_id, 0, $args );