How to Hard Code Custom menu items

The Problem with your code is that its not actually adding the links to the menu and only to the menu’s output, hence the use of a filter (add_filter) so you are just filtering the output of the menu in fact even if you don’t have a menu your link will be shown with the code you are using. But to create a link and add it to a menu you can use this code:

$run_once = get_option('menu_check');
if (!$run_once){
    //give your menu a name
    $name="theme default menu";
    //create the menu
    $menu_id = wp_create_nav_menu($name);
    //then get the menu object by its name
    $menu = get_term_by( 'name', $name, 'nav_menu' );

    //then add the actuall link/ menu item and you do this for each item you want to add
    wp_update_nav_menu_item($menu->term_id, 0, array(
        'menu-item-title' =>  __('Home'),
        'menu-item-classes' => 'home',
        'menu-item-url' => home_url( "https://wordpress.stackexchange.com/" ), 
        'menu-item-status' => 'publish'));

    //then you set the wanted theme  location
    $locations = get_theme_mod('nav_menu_locations');
    $locations['main-menu'] = $menu->term_id;
    set_theme_mod( 'nav_menu_locations', $locations );

    // then update the menu_check option to make sure this code only runs once
    update_option('menu_check', true);
}

I commented all over to make it simpler.

To create a child page/sub page/second level menu (how ever you may call it), you just need to set the menu-item-parent-id in the new item for example:

//create the top level menu item (home)
$top_menu = wp_update_nav_menu_item($menu->term_id, 0, array( 
    'menu-item-title' =>  __('Home'),
    'menu-item-classes' => 'home',
    'menu-item-url' => home_url( "https://wordpress.stackexchange.com/" ), 
    'menu-item-status' => 'publish'
    'menu-item-parent-id' => 0,
    ));
//Sub menu item (first child)
$first_child = wp_update_nav_menu_item($menu->term_id, 0, array( 
    'menu-item-title' =>  __('First_Child'),
    'menu-item-classes' => 'home',
    'menu-item-url' => home_url( "https://wordpress.stackexchange.com/" ), 
    'menu-item-status' => 'publish'
    'menu-item-parent-id' => $top_menu,
    ));
//Sub Sub menu item (first child)
$Second_child = wp_update_nav_menu_item($menu->term_id, 0, array( 
    'menu-item-title' =>  __('Second_Child'),
    'menu-item-classes' => 'home',
    'menu-item-url' => home_url( "https://wordpress.stackexchange.com/" ), 
    'menu-item-status' => 'publish'
    'menu-item-parent-id' => $first_child,
    ));

also you can set the position by code with menu-item-position
and i think its done like this:

  • First item – ‘menu-item-position’ => 1
    • First item first child – ‘menu-item-position’ => 1
    • First item second child – ‘menu-item-position’ => 1
      • First item second child first child – ‘menu-item-position’ => 1
  • Second item – ‘menu-item-position’ => 2
  • 3rd item – ‘menu-item-position’ => 3
  • 4th item – ‘menu-item-position’ => 4

Leave a Comment