Make WordPress Core


Ignore:
Timestamp:
05/25/2010 01:49:56 PM (14 years ago)
Author:
nacin
Message:

Allow pages to be added automattically to menus. see #13447.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/nav-menu.php

    r14826 r14878  
    749749}
    750750
     751function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
     752    if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type )
     753        return;
     754    $auto_add = get_option( 'nav_menu_options' );
     755    if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) )
     756        return;
     757    $auto_add = $auto_add['auto_add'];
     758    if ( empty( $auto_add ) || ! is_array( $auto_add ) )
     759        return;
     760
     761    $args = array(
     762        'menu-item-object-id' => $post->ID,
     763        'menu-item-object' => $post->post_type,
     764        'menu-item-type' => 'post_type',
     765    );
     766   
     767    foreach ( $auto_add as $menu_id ) {
     768        $items = (array) wp_get_nav_menu_items( $menu_id );
     769        foreach ( $items as $item ) {
     770            if ( $post->ID == $item->object_id )
     771                continue 2;
     772        }
     773        wp_update_nav_menu_item( $menu_id, 0, $args );
     774    }
     775}
     776add_action( 'transition_post_status', '_wp_auto_add_pages_to_menu', 10, 3 );
     777
    751778?>
Note: See TracChangeset for help on using the changeset viewer.