I have been trying to figure this out for hours and I've had no luck. I know it's something easy that I'm missing, but I cannot find it anywhere on StackOverflow.
What I want to is add the page that is being created here to either the existing menu (which will be blank), or remove the current menu, create a new menu and add the page that way, whichever is easier.
Here is the code I'm using to add a page (and editing / saving) from a plugin interface:
$titlep2 = $_REQUEST['titlep2'];
$post_text2 = $_REQUEST['post_text2'];
if($titlep2 != ""){
$posthwe_id2 = get_option("hwepag2");
if($posthwe_id2 == ""){
$hwea2 = array('post_title' => $titlep2,
'post_content' => $post_text2,
'post_status' => 'publish',
'post_type' => 'page');
$post_idp2 = wp_insert_post($hwea2);
update_option("hwepag2",$post_idp2);
}else{
$my_post2 = array('ID' => $posthwe_id2,
'post_title' => $titlep2,
'post_content' => $post_text2,
);
wp_update_post( $my_post2 );
}
}
I have tried many variations of wp_update_nav_menu_item, but I cannot get it to work. I have an existing menu, but it will be blank when adding this post (this is for new/fresh sites that I develop offline). That being the case, it seems the menuID, which is "menu-top" does not exist in the UL before adding an element to the menu.
So, it's fine if the current menu needs to be deleted, a new one created, then the page I'm adding added to that menu.
Thank you so much!