_wp_auto_add_pages_to_menu

Automatically add newly published page objects to menus with that as an option.

Description

_wp_auto_add_pages_to_menu( (string) $new_status, (string) $old_status, (object) $post ); 

Parameters (3)

0. $new_status (string)
The new status of the post object.
1. $old_status (string)
The old status of the post object.
2. $post (object)
The post object being transitioned from one status to another.

Usage

  1. if ( !function_exists( '_wp_auto_add_pages_to_menu' ) ) { 
  2. require_once ABSPATH . WPINC . '/nav-menu.php'; 
  3.  
  4. // The new status of the post object. 
  5. $new_status = ''; 
  6.  
  7. // The old status of the post object. 
  8. $old_status = ''; 
  9.  
  10. // The post object being transitioned from one status to another. 
  11. $post = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = _wp_auto_add_pages_to_menu($new_status, $old_status, $post); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/nav-menu.php  
  1. function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) { 
  2. if ( 'publish' != $new_status || 'publish' == $old_status || 'page' != $post->post_type ) 
  3. return; 
  4. if ( ! empty( $post->post_parent ) ) 
  5. return; 
  6. $auto_add = get_option( 'nav_menu_options' ); 
  7. if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) ) 
  8. return; 
  9. $auto_add = $auto_add['auto_add']; 
  10. if ( empty( $auto_add ) || ! is_array( $auto_add ) ) 
  11. return; 
  12.  
  13. $args = array( 
  14. 'menu-item-object-id' => $post->ID,  
  15. 'menu-item-object' => $post->post_type,  
  16. 'menu-item-type' => 'post_type',  
  17. 'menu-item-status' => 'publish',  
  18. ); 
  19.  
  20. foreach ( $auto_add as $menu_id ) { 
  21. $items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish, draft' ) ); 
  22. if ( ! is_array( $items ) ) 
  23. continue; 
  24. foreach ( $items as $item ) { 
  25. if ( $post->ID == $item->object_id ) 
  26. continue 2; 
  27. wp_update_nav_menu_item( $menu_id, 0, $args );