Here's my pickle. I'm using a script to automatically create a fully pledged WordPress site. In this process new pages, etc are created automatically. I now need a way to add the newly created page(s) to an existing menu on site from the script (this menu was created automatically by the theme I us and is pre-filled with links to some demo pages).

The script has full access to all WordPress functions, but I cannot use hooks (since this is all run before the site is even up). Is there a way to pull an existing menu into the script and change it's contents?

EDIT: In regards to the possible duplicate, the other article provides a way to create a new menu. I'm in the market for a way to recover and edit an existing menu to change it's layout and add items to it. :)

Thanks in advance! :)

  • this can be done with a plugin. have you ever create a plugin ? codex.wordpress.org/Writing_a_Plugin
    – mmm
    Mar 16, 2016 at 7:30
  • I've created quite a few plugins in my life, it's kinda what I do for living XD, but thing is, I can't use a plugin in this case because this code gets executed BEFORE the site is live, so the plugin won't even be live yet. :/ Mar 16, 2016 at 7:35
  • if the script initialize WordPress, it can activate a plugin too, no ?
    – mmm
    Mar 16, 2016 at 7:42
  • 3
    Possible duplicate of Programmatically add a Navigation menu and menu items
    – RRikesh
    Mar 16, 2016 at 8:33
  • 1
    Can't you use WP-CLI for the project or you have some project restrictions? Mar 16, 2016 at 10:25

1 Answer 1


I've found a solution to my problem by going over the documentation again and again, looking through source files and just generally running the same section of code over and over to scrutinize the output.

This is the solution I came up with, which works just perfectly for my needs:

//  Get a list of all the menus that are part of the site
    $menu_list = wp_get_nav_menus();
    $menu_id = -1;
//  Iterate over all the menus...
    foreach( $menu_list as $menu ) {
    //  ...and find the one you want...
        if( $menu->name === 'Main Menu' ) {
        //  ...and get it's ID.
            $menu_id = $menu->term_id;

//  Delete everything except the Home entry
    $menu_content = wp_get_nav_menu_items( 'Main Menu' );
    foreach( $menu_content as $menu_item ) {
        if($menu_item->title !== 'Home') {


    *   Menu building code
    *   @since 0.4.0pre1
    $menu_data = array(
                        'menu-item-title' => ( $menu_text !== '' ) ? wp_strip_all_tags( $menu_text ) : wp_strip_all_tags( $page_title ),
                        'menu-item-object' => 'page',
                        'menu-item-parent-id' => ( $parent !== '' || $parent !== 'none' ) ? intval( $parent ) : 0,
                        'menu-item-position'  => ( $menu_position !== '' ) ? ( intval( $menu_position ) === 1 ? 2 : intval( $menu_position ) ): 2,
                        'menu-item-object-id' => $post_id,
                        'menu-item-type' => 'post_type',
                        'menu-item-status' => 'publish'

//  Add this menu item to the existing nav menu if it is set to visible
    if( intval( $menu_visible ) !== false ) {
        wp_update_nav_menu_item( $menu_id, 0, $menu_data );

Feel free to suggest any improvements to the code, or any other suggestions :)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.