Corrupted nesting on the menu copy
-
Problem:
The copy of the multi nested menu has wrong neasting.Reason:
wp_get_nav_menu_items() returns the menu items in the order the items were created and it can happend that in the returned array the child is set before the parent. And that messes up the $rel lookup array.Solution:
The duplicate() function has to be change so:
In the first run we create the copy of all the menu items and the $rel lookup table with the $key as original menu item ID and the $value as the translated/new menu item ID.
In the second run we go trough every item of the new menu and if it has menu_item_parent ID we look it up in the $rel table ($key) and get it’s new menu item ID translation and write it insted of the old menu_item_parent of the new menu itemThe modified function:
function duplicate( $id = null, $name = null ) { // sanity check if( empty( $id ) || empty( $name ) ) return false; $id = intval( $id ); $name = sanitize_text_field( $name ); $source = wp_get_nav_menu_object( $id ); $source_items = wp_get_nav_menu_items( $id ); $new_id = wp_create_nav_menu( $name ); if( !$new_id ) return false; // key is the original db ID, val is the new $rel = array(); $i = 1; foreach( $source_items as $menu_item ) { $args = array( 'menu-item-db-id' => $menu_item->db_id, 'menu-item-object-id' => $menu_item->object_id, 'menu-item-object' => $menu_item->object, 'menu-item-position' => $i, 'menu-item-type' => $menu_item->type, 'menu-item-title' => $menu_item->title, 'menu-item-url' => $menu_item->url, 'menu-item-description' => $menu_item->description, 'menu-item-attr-title' => $menu_item->attr_title, 'menu-item-target' => $menu_item->target, 'menu-item-classes' => implode( ' ', $menu_item->classes ), 'menu-item-xfn' => $menu_item->xfn, 'menu-item-status' => $menu_item->post_status ); $parent_id = wp_update_nav_menu_item( $new_id, 0, $args ); $rel[$menu_item->db_id] = $parent_id; $i++; } $target_items = wp_get_nav_menu_items( $new_id ); $i = 0; foreach( $target_items as $menu_item ) { $source_menu_item = $source_items[$i]; //old/source menu item // did it have a parent? if so, we need to update with the NEW ID if( $source_menu_item->menu_item_parent ) { $parent_id = $rel[$source_menu_item->menu_item_parent]; $args = array( 'menu-item-db-id' => $source_menu_item->db_id, 'menu-item-object-id' => $source_menu_item->object_id, 'menu-item-object' => $source_menu_item->object, 'menu-item-position' => $i+1, 'menu-item-type' => $source_menu_item->type, 'menu-item-title' => $source_menu_item->title, 'menu-item-url' => $source_menu_item->url, 'menu-item-description' => $source_menu_item->description, 'menu-item-attr-title' => $source_menu_item->attr_title, 'menu-item-target' => $source_menu_item->target, 'menu-item-classes' => implode( ' ', $source_menu_item->classes ), 'menu-item-xfn' => $source_menu_item->xfn, 'menu-item-status' => $source_menu_item->post_status, 'menu-item-parent-id' => $parent_id ); $parent_id = wp_update_nav_menu_item( $new_id, $menu_item->db_id, $args ); } $i++; } return $new_id; }
- The topic ‘Corrupted nesting on the menu copy’ is closed to new replies.