1

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.

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 :-)

1 Answer 1

2

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.

4
  • I can't tell you how appreciative I am to see ANY signs of life on this question. One thing though, since this is a filter, I'm not sure its what I'm looking for. I need to be able to create a menu via script once, during plugin activation so that its just as if I created it manually on the site, and it persists without a runtime filter. Does this make sense?
    – Scott B
    Commented Mar 26, 2011 at 22:44
  • I've updated my question to clarify a bit. I can see that it was not clear on the aspect of all of this being done on plugin activation (once and done).
    – Scott B
    Commented Mar 26, 2011 at 22:52
  • @Scott: i updated my answer in short because its really late here and I'm half a sleep, but it should get you somewhere.
    – Bainternet
    Commented Mar 26, 2011 at 23:15
  • +1 for your help thus far. You gave me 100% more than I had before you answered :-)
    – Scott B
    Commented Mar 27, 2011 at 0:25

Your Answer

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

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