1

Possible Duplicate:
Can I add pages to my custom menu via script?

My theme has a custom menu called "Custom Header Menu" and i've got a plugin that sets up site defaults and creates my static pages that I'd like to customize in order to seed the custom menu with posts and pages on the fly.

So I'd like to add some code to the plugin so that each of the page's I'm creating gets added/assigned to the custom menu.

I'm just looking for a point of reference on how to add pages to a custom menu on the fly via script...

From wp-includes/nav-menu.php, I'm expecting to use this...

wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item_data = array() )

Where $menu_id is the id of my "Custom Header Menu" and $menu_item_data is an array of the pages I'm assigning to the menu.

2
  • Look at the core file class. It's inside /wp-includes/ folder. One is named class.something.php the other one just nav-menu.php or something like that. You got everything in there. For other solutions search WA. Already a lot of Qs around this and surely your A too.
    – kaiser
    Commented Mar 31, 2011 at 16:08
  • @Kaiser, thanks for the pointer to nav-menu.php - I think I'm on the right track with that one. I've updated my question with the relevant function I'll be coding against...
    – Scott B
    Commented Mar 31, 2011 at 16:29

2 Answers 2

5

I guess it doesn't need anymore explanation:

$mymenu = wp_get_nav_menu_object('Main Navigation Menu');
$menuID = (int) $mymenu->term_id;
$myPage = get_page_by_title('About Us');

$itemData =  array(
    'menu-item-object-id' => $myPage->ID,
    'menu-item-parent-id' => 0,
    'menu-item-position'  => 2,
    'menu-item-object' => 'page',
    'menu-item-type'      => 'post_type',
    'menu-item-status'    => 'publish'
  );

wp_update_nav_menu_item($menuID, 0, $itemData);
2
  • Testing this now. Thanks! btw, I'm assuming "Main Navigation Menu" is the name given to the menu when it was registered in the theme? register_nav_menus(array('header-menu' => __('Main Navigation Menu')));
    – Scott B
    Commented Mar 31, 2011 at 19:49
  • 2
    @Scott B - to be honest: i really don't appreciate your behavior in here. Sorry, but i have to say this. You answered only your own Qs, didn't help a single other user in here. Further more you don't give me the impression that you tried to really solve the Q yourself - this isn't meant to be your free "get my task done" forum/list. You're one Q away from 200 Qs and give me comments with content like "one thing I'm missing (...)". Imo: Get your task done yourself or pay someone for it.
    – kaiser
    Commented Apr 1, 2011 at 1:55
1

Can you post the name of your plugin? Maybe someone has already a solution for this specific question.

I cannot give u a solution. Haven't much played around with the "new" wp-navmenu feature so I can only make some suggestions:

  1. Search in your plugin for the function/method which creates a new post in the WordPress table.
  2. Return the id of the created post (wp_insert_post(), wp_update_post() returns it by default).
  3. Add the post with the given id to the menu:
    • With a post in wp_posts table - The wp-nav-menu items are simple posts with post type "wp-nav-item". You also need to add the right terms and make the relationship in the wp_term_relationships table.
    • With a filter-hook, e.g. 'wp_get_nav_menu_items'. Look at the file "nav-menu.php" in the wp-includes directory. On line 525 you could see the assigned filter-hook in the function "wp_get_nav_menu_items". This method wouldn't be parmanent, which meens you cannot edit the posts/pages in the "Menu"-Section in your Adminpanel. So option Nr.1 is probably the way to go for your problem.
1
  • thanks for your help. I've updated my question with the relevant code from nav-menu.php
    – Scott B
    Commented Mar 31, 2011 at 16:32

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