0

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!

1 Answer 1

1

Try this if not already done: Go to Appearance > Menus > Select your menu if you have multiple of them > Check the checkbox down below that says "Automatically add new top-level pages to this menu"

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry but that's not a viable solution... I'm adding multiple pages at once to my development sites automatically, including a home/front page and I want to specify which one is added. It's only the "Contact" page that I want added. But that contact page will have a different "page ID" and potentially name every time. Which is why it must be identified and added at the time of creation.
Okay, then try checking out this link : gist.github.com/eduwass/4dc3f4522f6a91d033d4
Already tried, it didn't work for me... plus since the menu is empty at the time of page creation, $menu_id NavMenu ID... I don't think it has a menu ID. There's nothing in the source (in the UL), only when there are items in the menu does it have an ID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.