I want to create my custom nav menu using a script in a wp plugin.
I’m almost there using this tutorial. All I need is the option to delete menu entries.
something like wp_delete_nav_menu_item($menuID, $itemID);
As an alternative, I could create a new menu using wp_create_nav_menu($menu_name); so I don”t have to delete the default entries of the menu I would be working with otherwise. The problem here is, that the theme gives options for the position of the menu like ‘header’ and I dont know how i can define a position for a newly created menu.
Hope someone can give advice so I can solve one of these issues.
Thanks!
This is the code I am working with so far. I need the code for the part to delete the default entries
//Add Menu
//Get Menu ID
$mymenu = wp_get_nav_menu_object('Header');
$menuID = $mymenu->term_id;
//Check if Menu exists
if( !$mymenu ) {
//Menu exists -> Delete the default entries
//
} else {
//Menu does not exist -> Create it
$menuID = wp_create_nav_menu('Header');
}
//Create new Menu Entries
//Create Parent Menu Entries
$myPage = get_page_by_title( 'Home' );
$itemData = array(
'menu-item-object-id' => $myPage->ID,
'menu-item-parent-id' => 0,
'menu-item-position' => 1,
'menu-item-object' => 'page',
'menu-item-type' => 'post_type',
'menu-item-status' => 'publish'
);
wp_update_nav_menu_item($menuID, 0, $itemData);
Here you go.
But we haven’t defined any location in theme yet.
This code will be used in theme where you want your menu to get rendered.