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] );
99 * Register a navigation menu for a theme.
103 * @param string $location Menu location identifier, like a slug.
104 * @param string $description Menu location descriptive text.
106 function register_nav_menu( $location, $description ) {
107 register_nav_menus( array( $location => $description ) );
110 * Returns an array of all registered navigation menus in a theme
115 function get_registered_nav_menus() {
116 global $_wp_registered_nav_menus;
117 if ( isset( $_wp_registered_nav_menus ) )
118 return $_wp_registered_nav_menus;
123 * Returns an array with the registered navigation menu locations and the menu assigned to it
129 function get_nav_menu_locations() {
130 $locations = get_theme_mod( 'nav_menu_locations' );
131 return ( is_array( $locations ) ) ? $locations : array();
135 * Whether a registered nav menu location has a menu assigned to it.
138 * @param string $location Menu location identifier.
139 * @return bool Whether location has a menu.
141 function has_nav_menu( $location ) {
142 $locations = get_nav_menu_locations();
143 return ( ! empty( $locations[ $location ] ) );
147 * Determine whether the given ID is a nav menu item.
151 * @param int $menu_item_id The ID of the potential nav menu item.
152 * @return bool Whether the given ID is that of a nav menu item.
154 function is_nav_menu_item( $menu_item_id = 0 ) {
155 return ( ! is_wp_error( $menu_item_id ) && ( 'nav_menu_item' == get_post_type( $menu_item_id ) ) );
159 * Create a Navigation Menu.
163 * @param string $menu_name Menu Name
164 * @return mixed Menu object on success|WP_Error on failure
166 function wp_create_nav_menu( $menu_name ) {
167 return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
171 * Delete a Navigation Menu.
175 * @param string $menu name|id|slug
176 * @return mixed Menu object on success|WP_Error on failure
178 function wp_delete_nav_menu( $menu ) {
179 $menu = wp_get_nav_menu_object( $menu );
183 $menu_objects = get_objects_in_term( $menu->term_id, 'nav_menu' );
184 if ( ! empty( $menu_objects ) ) {
185 foreach ( $menu_objects as $item ) {
186 wp_delete_post( $item );
190 $result = wp_delete_term( $menu->term_id, 'nav_menu' );
192 // Remove this menu from any locations.
193 $locations = get_nav_menu_locations();
194 foreach ( $locations as $location => $menu_id ) {
195 if ( $menu_id == $menu->term_id )
196 $locations[ $location ] = 0;
198 set_theme_mod( 'nav_menu_locations', $locations );
200 if ( $result && !is_wp_error($result) )
201 do_action( 'wp_delete_nav_menu', $menu->term_id );
207 * Save the properties of a menu or create a new menu with those properties.
211 * @param int $menu_id The ID of the menu or "0" to create a new menu.
212 * @param array $menu_data The array of menu data.
213 * @return int|error object The menu's ID or WP_Error object.
215 function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
216 $menu_id = (int) $menu_id;
218 $_menu = wp_get_nav_menu_object( $menu_id );
221 'description' => ( isset( $menu_data['description'] ) ? $menu_data['description'] : '' ),
222 'name' => ( isset( $menu_data['menu-name'] ) ? $menu_data['menu-name'] : '' ),
223 'parent' => ( isset( $menu_data['parent'] ) ? (int) $menu_data['parent'] : 0 ),
227 // double-check that we're not going to have one menu take the name of another
228 $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
230 $_possible_existing &&
231 ! is_wp_error( $_possible_existing ) &&
232 isset( $_possible_existing->term_id ) &&
233 $_possible_existing->term_id != $menu_id
235 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'] ) ) );
237 // menu doesn't already exist, so create a new menu
238 if ( ! $_menu || is_wp_error( $_menu ) ) {
239 $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
242 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'] ) ) );
244 $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );
246 if ( is_wp_error( $_menu ) )
249 do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );
251 return (int) $_menu['term_id'];
254 if ( ! $_menu || ! isset( $_menu->term_id ) )
257 $menu_id = (int) $_menu->term_id;
259 $update_response = wp_update_term( $menu_id, 'nav_menu', $args );
261 if ( is_wp_error( $update_response ) )
262 return $update_response;
264 do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
269 * Save the properties of a menu item or create a new one.
273 * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan.
274 * @param int $menu_item_db_id The ID of the menu item. If "0", creates a new menu item.
275 * @param array $menu_item_data The menu item's data.
276 * @return int The menu item's database ID or WP_Error object on failure.
278 function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() ) {
279 $menu_id = (int) $menu_id;
280 $menu_item_db_id = (int) $menu_item_db_id;
282 // make sure that we don't convert non-nav_menu_item objects into nav_menu_item objects
283 if ( ! empty( $menu_item_db_id ) && ! is_nav_menu_item( $menu_item_db_id ) )
284 return new WP_Error('update_nav_menu_item_failed', __('The given object ID is not that of a menu item.'));
286 $menu = wp_get_nav_menu_object( $menu_id );
288 if ( ( ! $menu && 0 !== $menu_id ) || is_wp_error( $menu ) )
292 'menu-item-db-id' => $menu_item_db_id,
293 'menu-item-object-id' => 0,
294 'menu-item-object' => '',
295 'menu-item-parent-id' => 0,
296 'menu-item-position' => 0,
297 'menu-item-type' => 'custom',
298 'menu-item-title' => '',
299 'menu-item-url' => '',
300 'menu-item-description' => '',
301 'menu-item-attr-title' => '',
302 'menu-item-target' => '',
303 'menu-item-classes' => '',
304 'menu-item-xfn' => '',
305 'menu-item-status' => '',
308 $args = wp_parse_args( $menu_item_data, $defaults );
310 if ( 0 == $menu_id ) {
311 $args['menu-item-position'] = 1;
312 } elseif ( 0 == (int) $args['menu-item-position'] ) {
313 $menu_items = 0 == $menu_id ? array() : (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
314 $last_item = array_pop( $menu_items );
315 $args['menu-item-position'] = ( $last_item && isset( $last_item->menu_order ) ) ? 1 + $last_item->menu_order : count( $menu_items );
318 $original_parent = 0 < $menu_item_db_id ? get_post_field( 'post_parent', $menu_item_db_id ) : 0;
320 if ( 'custom' != $args['menu-item-type'] ) {
321 /* if non-custom menu item, then:
322 * use original object's URL
323 * blank default title to sync with original object's
326 $args['menu-item-url'] = '';
328 $original_title = '';
329 if ( 'taxonomy' == $args['menu-item-type'] ) {
330 $original_parent = get_term_field( 'parent', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
331 $original_title = get_term_field( 'name', $args['menu-item-object-id'], $args['menu-item-object'], 'raw' );
332 } elseif ( 'post_type' == $args['menu-item-type'] ) {
334 $original_object = get_post( $args['menu-item-object-id'] );
335 $original_parent = (int) $original_object->post_parent;
336 $original_title = $original_object->post_title;
339 if ( $args['menu-item-title'] == $original_title )
340 $args['menu-item-title'] = '';
342 // hack to get wp to create a post object when too many properties are empty
343 if ( '' == $args['menu-item-title'] && '' == $args['menu-item-description'] )
344 $args['menu-item-description'] = ' ';
347 // Populate the menu item object
349 'menu_order' => $args['menu-item-position'],
351 'post_content' => $args['menu-item-description'],
352 'post_excerpt' => $args['menu-item-attr-title'],
353 'post_parent' => $original_parent,
354 'post_title' => $args['menu-item-title'],
355 'post_type' => 'nav_menu_item',
358 $update = 0 != $menu_item_db_id;
360 // Only set the menu term if it isn't set to avoid unnecessary wp_get_object_terms()
361 if ( $menu_id && ( ! $update || ! is_object_in_term( $menu_item_db_id, 'nav_menu', (int) $menu->term_id ) ) )
362 $post['tax_input'] = array( 'nav_menu' => array( intval( $menu->term_id ) ) );
364 // New menu item. Default is draft status
367 $post['post_status'] = 'publish' == $args['menu-item-status'] ? 'publish' : 'draft';
368 $menu_item_db_id = wp_insert_post( $post );
369 if ( ! $menu_item_db_id || is_wp_error( $menu_item_db_id ) )
370 return $menu_item_db_id;
373 if ( 'custom' == $args['menu-item-type'] ) {
374 $args['menu-item-object-id'] = $menu_item_db_id;
375 $args['menu-item-object'] = 'custom';
378 $menu_item_db_id = (int) $menu_item_db_id;
380 update_post_meta( $menu_item_db_id, '_menu_item_type', sanitize_key($args['menu-item-type']) );
381 update_post_meta( $menu_item_db_id, '_menu_item_menu_item_parent', strval( (int) $args['menu-item-parent-id'] ) );
382 update_post_meta( $menu_item_db_id, '_menu_item_object_id', strval( (int) $args['menu-item-object-id'] ) );
383 update_post_meta( $menu_item_db_id, '_menu_item_object', sanitize_key($args['menu-item-object']) );
384 update_post_meta( $menu_item_db_id, '_menu_item_target', sanitize_key($args['menu-item-target']) );
386 $args['menu-item-classes'] = array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-classes'] ) );
387 $args['menu-item-xfn'] = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['menu-item-xfn'] ) ) );
388 update_post_meta( $menu_item_db_id, '_menu_item_classes', $args['menu-item-classes'] );
389 update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
390 update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw($args['menu-item-url']) );
393 update_post_meta( $menu_item_db_id, '_menu_item_orphaned', (string) time() );
394 elseif ( get_post_meta( $menu_item_db_id, '_menu_item_orphaned' ) )
395 delete_post_meta( $menu_item_db_id, '_menu_item_orphaned' );
397 // Update existing menu item. Default is publish status
399 $post['ID'] = $menu_item_db_id;
400 $post['post_status'] = 'draft' == $args['menu-item-status'] ? 'draft' : 'publish';
401 wp_update_post( $post );
404 do_action('wp_update_nav_menu_item', $menu_id, $menu_item_db_id, $args );
406 return $menu_item_db_id;
410 * Returns all navigation menu objects.
414 * @param array $args Array of arguments passed on to get_terms().
415 * @return array menu objects
417 function wp_get_nav_menus( $args = array() ) {
418 $defaults = array( 'hide_empty' => false, 'orderby' => 'none' );
419 $args = wp_parse_args( $args, $defaults );
420 return apply_filters( 'wp_get_nav_menus', get_terms( 'nav_menu', $args), $args );
424 * Sort menu items by the desired key.
429 * @param object $a The first object to compare
430 * @param object $b The second object to compare
431 * @return int -1, 0, or 1 if $a is considered to be respectively less than, equal to, or greater than $b.
433 function _sort_nav_menu_items( $a, $b ) {
434 global $_menu_item_sort_prop;
436 if ( empty( $_menu_item_sort_prop ) )
439 if ( ! isset( $a->$_menu_item_sort_prop ) || ! isset( $b->$_menu_item_sort_prop ) )
442 $_a = (int) $a->$_menu_item_sort_prop;
443 $_b = (int) $b->$_menu_item_sort_prop;
445 if ( $a->$_menu_item_sort_prop == $b->$_menu_item_sort_prop )
447 elseif ( $_a == $a->$_menu_item_sort_prop && $_b == $b->$_menu_item_sort_prop )
448 return $_a < $_b ? -1 : 1;
450 return strcmp( $a->$_menu_item_sort_prop, $b->$_menu_item_sort_prop );
454 * Returns if a menu item is valid. Bug #13958
459 * @param object $menu_item The menu item to check
460 * @return bool false if invalid, else true.
462 function _is_valid_nav_menu_item( $item ) {
463 if ( ! empty( $item->_invalid ) )
470 * Returns all menu items of a navigation menu.
474 * @param string $menu menu name, id, or slug
475 * @param string $args
476 * @return mixed $items array of menu items, else false.
478 function wp_get_nav_menu_items( $menu, $args = array() ) {
479 $menu = wp_get_nav_menu_object( $menu );
484 static $fetched = array();
486 $items = get_objects_in_term( $menu->term_id, 'nav_menu' );
488 if ( empty( $items ) )
491 $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item',
492 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true );
493 $args = wp_parse_args( $args, $defaults );
494 if ( count( $items ) > 1 )
495 $args['include'] = implode( ',', $items );
497 $args['include'] = $items[0];
499 $items = get_posts( $args );
501 if ( is_wp_error( $items ) || ! is_array( $items ) )
504 // Get all posts and terms at once to prime the caches
505 if ( empty( $fetched[$menu->term_id] ) || wp_using_ext_object_cache() ) {
506 $fetched[$menu->term_id] = true;
509 foreach ( $items as $item ) {
510 $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
511 $object = get_post_meta( $item->ID, '_menu_item_object', true );
512 $type = get_post_meta( $item->ID, '_menu_item_type', true );
514 if ( 'post_type' == $type )
515 $posts[$object][] = $object_id;
516 elseif ( 'taxonomy' == $type)
517 $terms[$object][] = $object_id;
520 if ( ! empty( $posts ) ) {
521 foreach ( array_keys($posts) as $post_type ) {
522 get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) );
527 if ( ! empty( $terms ) ) {
528 foreach ( array_keys($terms) as $taxonomy ) {
529 get_terms($taxonomy, array('include' => $terms[$taxonomy]) );
535 $items = array_map( 'wp_setup_nav_menu_item', $items );
537 if ( ! is_admin() ) // Remove invalid items only in frontend
538 $items = array_filter( $items, '_is_valid_nav_menu_item' );
540 if ( ARRAY_A == $args['output'] ) {
541 $GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
542 usort($items, '_sort_nav_menu_items');
544 foreach( $items as $k => $item ) {
545 $items[$k]->$args['output_key'] = $i++;
549 return apply_filters( 'wp_get_nav_menu_items', $items, $menu, $args );
553 * Decorates a menu item object with the shared navigation menu item properties.
556 * - db_id: The DB ID of this item as a nav_menu_item object, if it exists (0 if it doesn't exist).
557 * - object_id: The DB ID of the original object this menu item represents, e.g. ID for posts and term_id for categories.
558 * - type: The family of objects originally represented, such as "post_type" or "taxonomy."
559 * - object: The type of object originally represented, such as "category," "post", or "attachment."
560 * - type_label: The singular label used to describe this type of menu item.
561 * - post_parent: The DB ID of the original object's parent object, if any (0 otherwise).
562 * - menu_item_parent: The DB ID of the nav_menu_item that is this item's menu parent, if any. 0 otherwise.
563 * - url: The URL to which this menu item points.
564 * - title: The title of this menu item.
565 * - target: The target attribute of the link element for this menu item.
566 * - attr_title: The title attribute of the link element for this menu item.
567 * - classes: The array of class attribute values for the link element of this menu item.
568 * - xfn: The XFN relationship expressed in the link of this menu item.
569 * - description: The description of this menu item.
573 * @param object $menu_item The menu item to modify.
574 * @return object $menu_item The menu item with standard menu item properties.
576 function wp_setup_nav_menu_item( $menu_item ) {
577 if ( isset( $menu_item->post_type ) ) {
578 if ( 'nav_menu_item' == $menu_item->post_type ) {
579 $menu_item->db_id = (int) $menu_item->ID;
580 $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;
581 $menu_item->object_id = empty( $menu_item->object_id ) ? get_post_meta( $menu_item->ID, '_menu_item_object_id', true ) : $menu_item->object_id;
582 $menu_item->object = empty( $menu_item->object ) ? get_post_meta( $menu_item->ID, '_menu_item_object', true ) : $menu_item->object;
583 $menu_item->type = empty( $menu_item->type ) ? get_post_meta( $menu_item->ID, '_menu_item_type', true ) : $menu_item->type;
585 if ( 'post_type' == $menu_item->type ) {
586 $object = get_post_type_object( $menu_item->object );
588 $menu_item->type_label = $object->labels->singular_name;
590 $menu_item->type_label = $menu_item->object;
591 $menu_item->_invalid = true;
594 $menu_item->url = get_permalink( $menu_item->object_id );
596 $original_object = get_post( $menu_item->object_id );
597 $original_title = $original_object->post_title;
598 $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
600 } elseif ( 'taxonomy' == $menu_item->type ) {
601 $object = get_taxonomy( $menu_item->object );
603 $menu_item->type_label = $object->labels->singular_name;
605 $menu_item->type_label = $menu_item->object;
606 $menu_item->_invalid = true;
609 $term_url = get_term_link( (int) $menu_item->object_id, $menu_item->object );
610 $menu_item->url = !is_wp_error( $term_url ) ? $term_url : '';
612 $original_title = get_term_field( 'name', $menu_item->object_id, $menu_item->object, 'raw' );
613 if ( is_wp_error( $original_title ) )
614 $original_title = false;
615 $menu_item->title = '' == $menu_item->post_title ? $original_title : $menu_item->post_title;
618 $menu_item->type_label = __('Custom');
619 $menu_item->title = $menu_item->post_title;
620 $menu_item->url = empty( $menu_item->url ) ? get_post_meta( $menu_item->ID, '_menu_item_url', true ) : $menu_item->url;
623 $menu_item->target = empty( $menu_item->target ) ? get_post_meta( $menu_item->ID, '_menu_item_target', true ) : $menu_item->target;
625 $menu_item->attr_title = empty( $menu_item->attr_title ) ? apply_filters( 'nav_menu_attr_title', $menu_item->post_excerpt ) : $menu_item->attr_title;
627 if ( empty( $menu_item->description ) )
628 $menu_item->description = apply_filters( 'nav_menu_description', wp_trim_words( $menu_item->post_content, 200 ) );
630 $menu_item->classes = empty( $menu_item->classes ) ? (array) get_post_meta( $menu_item->ID, '_menu_item_classes', true ) : $menu_item->classes;
631 $menu_item->xfn = empty( $menu_item->xfn ) ? get_post_meta( $menu_item->ID, '_menu_item_xfn', true ) : $menu_item->xfn;
633 $menu_item->db_id = 0;
634 $menu_item->menu_item_parent = 0;
635 $menu_item->object_id = (int) $menu_item->ID;
636 $menu_item->type = 'post_type';
638 $object = get_post_type_object( $menu_item->post_type );
639 $menu_item->object = $object->name;
640 $menu_item->type_label = $object->labels->singular_name;
642 if ( '' === $menu_item->post_title )
643 $menu_item->post_title = sprintf( __( '#%d (no title)' ), $menu_item->ID );
645 $menu_item->title = $menu_item->post_title;
646 $menu_item->url = get_permalink( $menu_item->ID );
647 $menu_item->target = '';
649 $menu_item->attr_title = apply_filters( 'nav_menu_attr_title', '' );
650 $menu_item->description = apply_filters( 'nav_menu_description', '' );
651 $menu_item->classes = array();
652 $menu_item->xfn = '';
654 } elseif ( isset( $menu_item->taxonomy ) ) {
655 $menu_item->ID = $menu_item->term_id;
656 $menu_item->db_id = 0;
657 $menu_item->menu_item_parent = 0;
658 $menu_item->object_id = (int) $menu_item->term_id;
659 $menu_item->post_parent = (int) $menu_item->parent;
660 $menu_item->type = 'taxonomy';
662 $object = get_taxonomy( $menu_item->taxonomy );
663 $menu_item->object = $object->name;
664 $menu_item->type_label = $object->labels->singular_name;
666 $menu_item->title = $menu_item->name;
667 $menu_item->url = get_term_link( $menu_item, $menu_item->taxonomy );
668 $menu_item->target = '';
669 $menu_item->attr_title = '';
670 $menu_item->description = get_term_field( 'description', $menu_item->term_id, $menu_item->taxonomy );
671 $menu_item->classes = array();
672 $menu_item->xfn = '';
676 return apply_filters( 'wp_setup_nav_menu_item', $menu_item );
680 * Get the menu items associated with a particular object.
684 * @param int $object_id The ID of the original object.
685 * @param string $object_type The type of object, such as "taxonomy" or "post_type."
686 * @param string $taxonomy If $object_type is "taxonomy", $taxonomy is the name of the tax that $object_id belongs to
687 * @return array The array of menu item IDs; empty array if none;
689 function wp_get_associated_nav_menu_items( $object_id = 0, $object_type = 'post_type', $taxonomy = '' ) {
690 $object_id = (int) $object_id;
691 $menu_item_ids = array();
693 $query = new WP_Query;
694 $menu_items = $query->query(
696 'meta_key' => '_menu_item_object_id',
697 'meta_value' => $object_id,
698 'post_status' => 'any',
699 'post_type' => 'nav_menu_item',
700 'posts_per_page' => -1,
703 foreach( (array) $menu_items as $menu_item ) {
704 if ( isset( $menu_item->ID ) && is_nav_menu_item( $menu_item->ID ) ) {
705 if ( get_post_meta( $menu_item->ID, '_menu_item_type', true ) !== $object_type ||
706 get_post_meta( $menu_item->ID, '_menu_item_object', true ) !== $taxonomy )
709 $menu_item_ids[] = (int) $menu_item->ID;
713 return array_unique( $menu_item_ids );
717 * Callback for handling a menu item when its original object is deleted.
722 * @param int $object_id The ID of the original object being trashed.
725 function _wp_delete_post_menu_item( $object_id = 0 ) {
726 $object_id = (int) $object_id;
728 $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'post_type' );
730 foreach( (array) $menu_item_ids as $menu_item_id ) {
731 wp_delete_post( $menu_item_id, true );
736 * Callback for handling a menu item when its original object is deleted.
741 * @param int $object_id The ID of the original object being trashed.
744 function _wp_delete_tax_menu_item( $object_id = 0, $tt_id, $taxonomy ) {
745 $object_id = (int) $object_id;
747 $menu_item_ids = wp_get_associated_nav_menu_items( $object_id, 'taxonomy', $taxonomy );
749 foreach( (array) $menu_item_ids as $menu_item_id ) {
750 wp_delete_post( $menu_item_id, true );
755 * Automatically add newly published page objects to menus with that as an option.
760 * @param string $new_status The new status of the post object.
761 * @param string $old_status The old status of the post object.
762 * @param object $post The post object being transitioned from one status to another.
765 function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
766 if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
768 if ( ! empty( $post->post_parent ) )
770 $auto_add = get_option( 'nav_menu_options' );
771 if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
773 $auto_add = $auto_add['auto_add'];
774 if ( empty( $auto_add ) || ! is_array( $auto_add ) )
778 'menu-item-object-id' => $post->ID,
779 'menu-item-object' => $post->post_type,
780 'menu-item-type' => 'post_type',
781 'menu-item-status' => 'publish',
784 foreach ( $auto_add as $menu_id ) {
785 $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
786 if ( ! is_array( $items ) )
788 foreach ( $items as $item ) {
789 if ( $post->ID == $item->object_id )
792 wp_update_nav_menu_item( $menu_id, 0, $args );