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 $locations = get_nav_menu_locations();
146 return ( ! empty( $locations[ $location ] ) );
150 * Determine whether the given ID is a nav menu item.
154 * @param int $menu_item_id The ID of the potential nav menu item.
155 * @return bool Whether the given ID is that of a nav menu item.
157 function is_nav_menu_item( $menu_item_id = 0 ) {
158 return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' == get_post_type( $menu_item_id ) ) );
162 * Create a Navigation Menu.
166 * @param string $menu_name Menu Name
167 * @return mixed Menu object on success|WP_Error on failure
169 function wp_create_nav_menu( $menu_name ) {
170 return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
174 * Delete a Navigation Menu.
178 * @param string $menu name|id|slug
179 * @return mixed Menu object on success|WP_Error on failure
181 function wp_delete_nav_menu( $menu ) {
182 $menu = wp_get_nav_menu_object( $menu );
186 $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
187 if ( ! empty( $menu_objects ) ) {
188 foreach ( $menu_objects as $item ) {
189 wp_delete_post( $item );
193 $result = wp_delete_term( $menu->term_id, 'nav_menu' );
195 // Remove this menu from any locations.
196 $locations = get_nav_menu_locations();
197 foreach ( $locations as $location => $menu_id ) {
198 if ( $menu_id == $menu->term_id )
199 $locations[ $location ] = 0;
201 set_theme_mod( 'nav_menu_locations', $locations );
203 if ( $result && !is_wp_error($result) )
206 * Fires after a navigation menu has been successfully deleted.
210 * @param int $term_id ID of the deleted menu.
212 do_action( 'wp_delete_nav_menu', $menu->term_id );
218 * Save the properties of a menu or create a new menu with those properties.
222 * @param int $menu_id The ID of the menu or "0" to create a new menu.
223 * @param array $menu_data The array of menu data.
224 * @return int|WP_Error object The menu's ID or WP_Error object.
226 function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
227 $menu_id = (int) $menu_id;
229 $_menu = wp_get_nav_menu_object( $menu_id );
232 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),
233 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),
234 'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),
238 // double-check that we're not going to have one menu take the name of another
239 $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
241 $_possible_existing &&
242 ! is_wp_error( $_possible_existing ) &&
243 isset( $_possible_existing->term_id ) &&
244 $_possible_existing->term_id != $menu_id
246 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'] ) ) );
248 // menu doesn't already exist, so create a new menu
249 if ( ! $_menu || is_wp_error( $_menu ) ) {
250 $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
253 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'] ) ) );
255 $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );
257 if ( is_wp_error( $_menu ) )
261 * Fires after a navigation menu is successfully created.
265 * @param int $term_id ID of the new menu.
266 * @param array $menu_data An array of menu data.
268 do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
270 return (int) $_menu['term_id'];
273 if ( ! $_menu || ! isset( $_menu->term_id ) )
276 $menu_id = (int) $_menu->term_id;
278 $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
280 if ( is_wp_error( $update_response ) )
281 return $update_response;
284 * Fires after a navigation menu has been successfully updated.
288 * @param int $menu_id ID of the updated menu.
289 * @param array $menu_data An array of menu data.
291 do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
296 * Save the properties of a menu item or create a new one.
300 * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
301 * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
302 * @param array $menu_item_data The menu item's data.
303 * @return int|WP_Error The menu item's database ID or WP_Error object on failure.
305 function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) {
306 $menu_id = (int) $menu_id;
307 $menu_item_db_id = (int) $menu_item_db_id;
309 // make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects
310 if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) )
311 return new WP_Error( 'update_nav_menu_item_failed', __( 'The given object ID is not that of a menu item.' ) );
313 $menu = wp_get_nav_menu_object( $menu_id );
315 if ( ! $menu && 0 !== $menu_id ) {
316 return new WP_Error( 'invalid_menu_id', __( 'Invalid menu ID.' ) );
319 if ( is_wp_error( $menu ) ) {
324 'menu-item-db-id' => $menu_item_db_id,
325 'menu-item-object-id' => 0,
326 'menu-item-object' => '',
327 'menu-item-parent-id' => 0,
328 'menu-item-position' => 0,
329 'menu-item-type' => 'custom',
330 'menu-item-title' => '',
331 'menu-item-url' => '',
332 'menu-item-description' => '',
333 'menu-item-attr-title' => '',
334 'menu-item-target' => '',
335 'menu-item-classes' => '',
336 'menu-item-xfn' => '',
337 'menu-item-status' => '',
340 $args = wp_parse_args( $menu_item_data, $defaults );
342 if ( 0 == $menu_id ) {
343 $args['menu-item-position'] = 1;
344 } elseif ( 0 == (int) $args['menu-item-position'] ) {
345 $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
346 $last_item = array_pop( $menu_items );
347 $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items );
350 $original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;
352 if ( 'custom' != $args['menu-item-type'] ) {
353 /* if non-custom menu item, then:
354 * use original object's URL
355 * blank default title to sync with original object's
358 $args['menu-item-url'] = '';
360 $original_title = '';
361 if ( 'taxonomy' == $args['menu-item-type'] ) {
362 $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
363 $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
364 } elseif ( 'post_type' == $args['menu-item-type'] ) {
366 $original_object = get_post( $args['menu-item-object-id'] );
367 $original_parent = (int) $original_object->post_parent;
368 $original_title = $original_object->post_title;
371 if ( $args['menu-item-title'] == $original_title )
372 $args['menu-item-title'] = '';
374 // hack to get wp to create a post object when too many properties are empty
375 if ( '' == $args['menu-item-title'] && '' == $args['menu-item-description'] )
376 $args['menu-item-description'] = ' ';
379 // Populate the menu item object
381 'menu_order' => $args['menu-item-position'],
383 'post_content' => $args['menu-item-description'],
384 'post_excerpt' => $args['menu-item-attr-title'],
385 'post_parent' => $original_parent,
386 'post_title' => $args['menu-item-title'],
387 'post_type' => 'nav_menu_item',
390 $update = 0 != $menu_item_db_id;
392 // New menu item. Default is draft status
395 $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
396 $menu_item_db_id = wp_insert_post( $post );
397 if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
398 return $menu_item_db_id;
401 // Associate the menu item with the menu term
402 // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
403 if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) ) {
404 wp_set_object_terms( $menu_item_db_id, array( $menu->term_id ), 'nav_menu' );
407 if ( 'custom' == $args['menu-item-type'] ) {
408 $args['menu-item-object-id'] = $menu_item_db_id;
409 $args['menu-item-object'] = 'custom';
412 $menu_item_db_id = (int) $menu_item_db_id;
414 update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']) );
415 update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', strval( (int) $args['menu-item-parent-id'] ) );
416 update_post_meta( $menu_item_db_id, '_menu_item_object_id', strval( (int) $args['menu-item-object-id'] ) );
417 update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']) );
418 update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']) );
420 $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) );
421 $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );
422 update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
423 update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
424 update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) );
427 update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
428 elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) )
429 delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
431 // Update existing menu item. Default is publish status
433 $post['ID'] = $menu_item_db_id;
434 $post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
435 wp_update_post( $post );
439 * Fires after a navigation menu item has been updated.
443 * @see wp_update_nav_menu_items()
445 * @param int $menu_id ID of the updated menu.
446 * @param int $menu_item_db_id ID of the updated menu item.
447 * @param array $args An array of arguments used to update a menu item.
449 do_action( 'wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
451 return $menu_item_db_id;
455 * Returns all navigation menu objects.
459 * @param array $args Array of arguments passed on to get_terms().
460 * @return array menu objects
462 function wp_get_nav_menus( $args = array() ) {
463 $defaults = array( 'hide_empty' => false, 'orderby' => 'none' );
464 $args = wp_parse_args( $args, $defaults );
467 * Filter the navigation menu objects being returned.
473 * @param array $menus An array of menu objects.
474 * @param array $args An array of arguments used to retrieve menu objects.
476 return apply_filters( 'wp_get_nav_menus', get_terms( 'nav_menu', $args), $args );
480 * Sort menu items by the desired key.
485 * @param object $a The first object to compare
486 * @param object $b The second object to compare
487 * @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b.
489 function _sort_nav_menu_items( $a, $b ) {
490 global $_menu_item_sort_prop;
492 if ( empty( $_menu_item_sort_prop ) )
495 if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) )
498 $_a = (int) $a->$_menu_item_sort_prop;
499 $_b = (int) $b->$_menu_item_sort_prop;
501 if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop )
503 elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop )
504 return $_a < $_b ? -1 : 1;
506 return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop );
510 * Returns if a menu item is valid. Bug #13958
515 * @param object $menu_item The menu item to check
516 * @return bool false if invalid, else true.
518 function _is_valid_nav_menu_item( $item ) {
519 if ( ! empty( $item->_invalid ) )
526 * Returns all menu items of a navigation menu.
530 * @param string $menu menu name, id, or slug
531 * @param string $args
532 * @return mixed $items array of menu items, else false.
534 function wp_get_nav_menu_items( $menu, $args = array() ) {
535 $menu = wp_get_nav_menu_object( $menu );
540 static $fetched = array();
542 $items = get_objects_in_term( $menu->term_id, 'nav_menu' );
544 if ( empty( $items ) )
547 $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',
548 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true );
549 $args = wp_parse_args( $args, $defaults );
550 if ( count( $items ) > 1 )
551 $args['include'] = implode( ',', $items );
553 $args['include'] = $items[0];
555 $items = get_posts( $args );
557 if ( is_wp_error( $items ) || ! is_array( $items ) )
560 // Get all posts and terms at once to prime the caches
561 if ( empty( $fetched[$menu->term_id] ) || wp_using_ext_object_cache() ) {
562 $fetched[$menu->term_id] = true;
565 foreach ( $items as $item ) {
566 $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
567 $object = get_post_meta( $item->ID, '_menu_item_object', true );
568 $type = get_post_meta( $item->ID, '_menu_item_type', true );
570 if ( 'post_type' == $type )
571 $posts[$object][] = $object_id;
572 elseif ( 'taxonomy' == $type)
573 $terms[$object][] = $object_id;
576 if ( ! empty( $posts ) ) {
577 foreach ( array_keys($posts) as $post_type ) {
578 get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) );
583 if ( ! empty( $terms ) ) {
584 foreach ( array_keys($terms) as $taxonomy ) {
585 get_terms($taxonomy, array('include' => $terms[$taxonomy]) );
591 $items = array_map( 'wp_setup_nav_menu_item', $items );
593 if ( ! is_admin() ) // Remove invalid items only in frontend
594 $items = array_filter( $items, '_is_valid_nav_menu_item' );
596 if ( ARRAY_A == $args['output'] ) {
597 $GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
598 usort($items, '_sort_nav_menu_items');
600 foreach( $items as $k => $item ) {
601 $items[$k]->$args['output_key'] = $i++;
606 * Filter the navigation menu items being returned.
610 * @param array $items An array of menu item post objects.
611 * @param object $menu The menu object.
612 * @param array $args An array of arguments used to retrieve menu item objects.
614 return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
618 * Decorates a menu item object with the shared navigation menu item properties.
621 * - db_id: The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
622 * - object_id: The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
623 * - type: The family of objects originally represented, such as "post_type" or "taxonomy."
624 * - object: The type of object originally represented, such as "category," "post", or "attachment."
625 * - type_label: The singular label used to describe this type of menu item.
626 * - post_parent: The DB ID of the original object's parent object, if any (0 otherwise).
627 * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
628 * - url: The URL to which this menu item points.
629 * - title: The title of this menu item.
630 * - target: The target attribute of the link element for this menu item.
631 * - attr_title: The title attribute of the link element for this menu item.
632 * - classes: The array of class attribute values for the link element of this menu item.
633 * - xfn: The XFN relationship expressed in the link of this menu item.
634 * - description: The description of this menu item.
638 * @param object $menu_item The menu item to modify.
639 * @return object $menu_item The menu item with standard menu item properties.
641 function wp_setup_nav_menu_item( $menu_item ) {
642 if ( isset( $menu_item->post_type ) ) {
643 if ( 'nav_menu_item' == $menu_item->post_type ) {
644 $menu_item->db_id = (int) $menu_item->ID;
645 $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;
646 $menu_item->object_id = empty( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
647 $menu_item->object = empty( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
648 $menu_item->type = empty( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;
650 if ( 'post_type' == $menu_item->type ) {
651 $object = get_post_type_object( $menu_item->object );
653 $menu_item->type_label = $object->labels->singular_name;
655 $menu_item->type_label = $menu_item->object;
656 $menu_item->_invalid = true;
659 $menu_item->url = get_permalink( $menu_item->object_id );
661 $original_object = get_post( $menu_item->object_id );
662 $original_title = $original_object->post_title;
663 $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
665 } elseif ( 'taxonomy' == $menu_item->type ) {
666 $object = get_taxonomy( $menu_item->object );
668 $menu_item->type_label = $object->labels->singular_name;
670 $menu_item->type_label = $menu_item->object;
671 $menu_item->_invalid = true;
674 $term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
675 $menu_item->url = !is_wp_error( $term_url ) ? $term_url : '';
677 $original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' );
678 if ( is_wp_error( $original_title ) )
679 $original_title = false;
680 $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
683 $menu_item->type_label = __('Custom');
684 $menu_item->title = $menu_item->post_title;
685 $menu_item->url = empty( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;
688 $menu_item->target = empty( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;
691 * Filter a navigation menu item's title attribute.
695 * @param string $item_title The menu item title attribute.
697 $menu_item->attr_title = empty( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;
699 if ( empty( $menu_item->description ) ) {
701 * Filter a navigation menu item's description.
705 * @param string $description The menu item description.
707 $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
710 $menu_item->classes = empty( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
711 $menu_item->xfn = empty( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
713 $menu_item->db_id = 0;
714 $menu_item->menu_item_parent = 0;
715 $menu_item->object_id = (int) $menu_item->ID;
716 $menu_item->type = 'post_type';
718 $object = get_post_type_object( $menu_item->post_type );
719 $menu_item->object = $object->name;
720 $menu_item->type_label = $object->labels->singular_name;
722 if ( '' === $menu_item->post_title )
723 $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
725 $menu_item->title = $menu_item->post_title;
726 $menu_item->url = get_permalink( $menu_item->ID );
727 $menu_item->target = '';
729 /** This filter is documented in wp-includes/nav-menu.php */
730 $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );
732 /** This filter is documented in wp-includes/nav-menu.php */
733 $menu_item->description = apply_filters( 'nav_menu_description', '' );
734 $menu_item->classes = array();
735 $menu_item->xfn = '';
737 } elseif ( isset( $menu_item->taxonomy ) ) {
738 $menu_item->ID = $menu_item->term_id;
739 $menu_item->db_id = 0;
740 $menu_item->menu_item_parent = 0;
741 $menu_item->object_id = (int) $menu_item->term_id;
742 $menu_item->post_parent = (int) $menu_item->parent;
743 $menu_item->type = 'taxonomy';
745 $object = get_taxonomy( $menu_item->taxonomy );
746 $menu_item->object = $object->name;
747 $menu_item->type_label = $object->labels->singular_name;
749 $menu_item->title = $menu_item->name;
750 $menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy );
751 $menu_item->target = '';
752 $menu_item->attr_title = '';
753 $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );
754 $menu_item->classes = array();
755 $menu_item->xfn = '';
760 * Filter a navigation menu item object.
764 * @param object $menu_item The menu item object.
766 return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
770 * Get the menu items associated with a particular object.
774 * @param int $object_id The ID of the original object.
775 * @param string $object_type The type of object, such as "taxonomy" or "post_type."
776 * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
777 * @return array The array of menu item IDs; empty array if none;
779 function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
780 $object_id = (int) $object_id;
781 $menu_item_ids = array();
783 $query = new WP_Query;
784 $menu_items = $query->query(
786 'meta_key' => '_menu_item_object_id',
787 'meta_value' => $object_id,
788 'post_status' => 'any',
789 'post_type' => 'nav_menu_item',
790 'posts_per_page' => -1,
793 foreach( (array) $menu_items as $menu_item ) {
794 if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
795 $menu_item_type = get_post_meta( $menu_item->ID, '_menu_item_type', true );
797 'post_type' == $object_type &&
798 'post_type' == $menu_item_type
800 $menu_item_ids[] = (int) $menu_item->ID;
802 'taxonomy' == $object_type &&
803 'taxonomy' == $menu_item_type &&
804 get_post_meta( $menu_item->ID, '_menu_item_object', true ) == $taxonomy
806 $menu_item_ids[] = (int) $menu_item->ID;
811 return array_unique( $menu_item_ids );
815 * Callback for handling a menu item when its original object is deleted.
820 * @param int $object_id The ID of the original object being trashed.
823 function _wp_delete_post_menu_item( $object_id = 0 ) {
824 $object_id = (int) $object_id;
826 $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
828 foreach( (array) $menu_item_ids as $menu_item_id ) {
829 wp_delete_post( $menu_item_id, true );
834 * Callback for handling a menu item when its original object is deleted.
839 * @param int $object_id The ID of the original object being trashed.
842 function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) {
843 $object_id = (int) $object_id;
845 $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
847 foreach( (array) $menu_item_ids as $menu_item_id ) {
848 wp_delete_post( $menu_item_id, true );
853 * Automatically add newly published page objects to menus with that as an option.
858 * @param string $new_status The new status of the post object.
859 * @param string $old_status The old status of the post object.
860 * @param object $post The post object being transitioned from one status to another.
863 function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
864 if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
866 if ( ! empty( $post->post_parent ) )
868 $auto_add = get_option( 'nav_menu_options' );
869 if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
871 $auto_add = $auto_add['auto_add'];
872 if ( empty( $auto_add ) || ! is_array( $auto_add ) )
876 'menu-item-object-id' => $post->ID,
877 'menu-item-object' => $post->post_type,
878 'menu-item-type' => 'post_type',
879 'menu-item-status' => 'publish',
882 foreach ( $auto_add as $menu_id ) {
883 $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
884 if ( ! is_array( $items ) )
886 foreach ( $items as $item ) {
887 if ( $post->ID == $item->object_id )
890 wp_update_nav_menu_item( $menu_id, 0, $args );