The WordPress functions changed since the answers here in 2014.
As of today (Version 4.6.1) this code will create a main menu named “My Menu” , main item and sub item.
To run code just paste and saves in your functions.php file in your child theme.
$menu_id = wp_create_nav_menu('My Menu');
$parent_item = wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => __('Main Page'),
'menu-item-url' => home_url( '/main-page/' ),
'menu-item-status' => 'publish',
)
);
wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => __('Sub Item Page'),
'menu-item-url' => home_url( '/sub-item-page/' ),
'menu-item-status' => 'publish',
'menu-item-parent-id' => $parent_item)
);
Docs:
Related Posts:
- Add an admin page, but don’t show it on the admin menu
- Custom menu walker: how can i check first item
- How to give position to Submenu under custom post type
- Primary and secondary menus
- how to get custom admin submenu( custom post type ) item to highlight when its active
- How can I add a sub menu to an existing navigation menu in WordPress?
- Programmatically adding menu items function replicates in multiple menus
- Change top level menu item to point to custom submenu item
- Walker nav remove submenu ul from a certain depth
- Admin Panel – Custom Menu Sub-Item LINK
- WordPress bootstrap menue with 3. level
- Insert & order custom post types in/as submenu
- Subnav menu – adapt to show the same on parent, child & grandchildren pages?
- disable menu collapse/expand function and make a menu item a static, unclickable text
- Submenu causes issue in defined blog page
- Define supporting only 1-level menu
- Adding a submenu to the Tools menu returns tools.php in a popup box
- Register submenu on network menu from the subsite
- Get the page IDs of a Particular Menu item’s submenu
- sub-menu does not show on the very top bar
- sub-page settings saving in NETWORK DASHBOARD
- How to hide Newsletter plugin submenus from the Dashboard?
- How to exclude/remove submenu using Walker_Nav_Menu
- Links doesnt appear as a submenu on a mobile
- Notices are render inside sub-menu’s content
- Bootstrap Theme Migrated to WordPress
- Problem add submenu page
- Submenu Page under Settings in Admin Panel
- Controlling the position of dropdow submenu
- How to have submenus collapsed until they are clicked?
- Assign index page to my mobile site?
- Restrict days in bootstrap datepicker using checkboxes in submenu
- Need help finding information to create this type of page
- How to keep the plugin submenu open on viewing a custom version of users.php?
- Adding Meta Box to Specific Submenu Page
- Trouble posting in Sub-Category
- Add a Submenu from Another Submenu in a Custom Theme
- Subpages List Appearing on Every Page
- edit-tags.php in plugin admin menu hides when is the active page
- Active class on side menu
- I need to get this sub nav to show up on cat, sup-cat and post
- Show menu on page with all sub-pages and sub-sub-pages of that page
- WordPress two row automatic sub navigation with double wp_list_pages. Is it impossible?
- Horizontal sub-menu on one line
- add_menu_page() with different name for first submenu item
- Display a portion/ branch of the menu tree using wp_nav_menu()
- Is It Possible To Add Custom Post Type Menu As Another Custom Post Type Sub Menu
- How to Add a Sub Menu Page to a Custom Post Type?
- How to add sub-menu to a menu generated by wp_nav_menu by using plugin
- Check if add_menu_page exists or not
- Remove Categories / Tags From Admin Menu
- WordPress API Menu/Submenu Order
- Plugin – create a page without it appearing in the side menu
- Getting custom admin submenu item to highlight when its active
- Add custom menu item using wp_nav_menu_items filter
- how to create a menu with all sub categories?
- Remove a menu item created by a plugin
- Add separator to admin submenu
- Display only page specific sub menu items using Custom Walker
- Add menu and submenu in admin with a URL instead of slug?
- How to add custom submenu links in wp-admin menus?
- Add custom html to last sub-menu item
- add_submenu_page() link missing ‘admin.php?’ prefix
- add_submenu_page not working
- Add a .last class to the last in each ul.sub-menu
- Order Admin sub-menu items?
- How to create sub menu with a URL parameter?
- How Can I remove or hide the export page in WordPress menu?
- Get page by template?
- Multi Level Bootstrap Navigation Menu in WordPress
- Add class to top level menu item if it has multiple child levels
- How to link a Custom Post Type submenu to a Nextgen Gallery page?
- How show sub menu only using wp_nav_menu()
- Show only 2nd level of navigation depending on active navigation
- Reorder custom submenu item
- Menu capability in WordPress
- Simple Navigation Walker – Wrapper-class around first sub-menu
- How Do I Programmatically Better Organize Custom Post Type Menus?
- How to get a separate child menu?
- Exclude one item from wp_list_pages( $args );
- The seventh parameter passed to add_submenu_page()
- Is there a way to keep a custom menu expanded when editing a custom post type?
- wp_nav_menu() loses ‘current-menu-*’ classes on single product page within category
- How to drag multiple menu items at once in WordPress?
- Customizing the a tag with Semantic UI
- Add Settings to Custom Post Type
- checkbox in post add/edit to add/remove the post from menu
- Put a wp_nav_menu inside another one
- Using custom post types in submenu + custom title
- I want only my sub-pages to be clickable in the navbar
- Hooking into add_submenu_page
- How to make pages metabox display all hierarchy in menu admin?
- Moving Categories submenu to Media, but still opens Posts menu
- Category menu item and its last 10 posts as sub-menu
- Add items to a menu dynamically
- How to add more than 1 user role to sub-menu pages
- Generate a Menu that displays all child pages of top level parent
- How to display submenus on page based on referring top-level parent?
- Odd behaviour with submenu link creation
- Prevent Admin gui output from page added using add_submenu_page