Can I add pages to my custom menu via script?

My theme has a custom menu assignment…

function my_register_my_menus() {
  register_nav_menus(
    array('header-menu' => __( 'Custom Header Menu' ) )
  );
}

…and I’m creating an “installer” type plugin that, upon activation, I would like to hook into this custom menu and assign some pages to it as if they had been created manually.

Read More

However, the menu API is pretty new and to date, I’ve been unable to find any examples of how to do this.

I’m hoping someone here can give me some direction, examples or info on how to do it.

Thanks in advance 🙂

Related posts

Leave a Reply

1 comment

  1. You can use wp_nav_menu_{$menu->slug}_items hook and add you link with a callback function, for example if your menu slug is header-menu then something like this:

    add_filter('wp_nav_menu_header-menu_items', 'add_my_extra_links',10,2);
    function add_my_extra_links($items, $args) {
      $newitems = '<li><a title="Test Link" href="http://google.com">Google</a></li>';
      $newitems .= '<li><a title="Test Link" href="http://yahoo.com">Yahoo</a></li>';
      $newitems .= '<li><a title="Test Link" href="http://bing.com">Bing</a></li>';
      $newitems .= $items;
      return $newitems;
    }
    

    Update

    I guess you would need to call wp_save_nav_menu_items which is the function used to save the menu items to the database.