Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

What's the easiest way to duplicate an entire navigation menu? I don't care if it involves SQL commands or whatnot. I just have some really big menus that I'd like to duplicate. Didn't see a plugin to do this.

For clarification, I wan to duplicate the menu so I can make some edits to it and use it as a secondary menu elsewhere. I'm not trying to display the menu twice.

wordpress.jpg

share|improve this question
    
elsewhere = a different site? –  onetrickpony Sep 21 '11 at 23:38
    
no, on the same site. –  cwd Sep 22 '11 at 0:21
    
If the menu will keep its structure, then I don't see any logical reason to duplicate it. You can use the same menu, and style it differently if that's what you want. –  onetrickpony Sep 22 '11 at 1:00
    
i guess the question is not clear enough or you guys are not reading. the question does say that I am going to make edits. anyhow, consider this scenario and tell me how you would best deal with it: you have a menu with 200 items and want to keep the first 100 in that menu while moving the next 100 to a second menu. the menu items are nested and some of them have custom titles, and custom links. my preferred method would be to duplicate the whole menu and then delete the ones i didn't need. however, it brings me to a fundamental question: what is the easiest way to duplicate a menu? –  cwd Sep 22 '11 at 4:24
1  
menu items are custom post type objects with a post_type of nav_menu_item. So you can query, modify and insert them exactly like (custom) post (types). –  kaiser Oct 10 '11 at 10:22
show 1 more comment

2 Answers

up vote 1 down vote accepted

UPDATE: The "easiest" way is to use my new plugin: http://wordpress.org/extend/plugins/enhanced-menu-editor/

It's not approved yet, but once it is I'll publish the widget. Not only does it allow for copying menus, but you can also synchronize the menu structure you establish in the menu editor to the page heirarchy of the pages themselves. So you can use drag-n-drop ajax coolness instead of editing potentially hundreds of pages manually.

OLD ANSWER:

The "easiest" way is to use code. There are two functions you'll want to use:

wp_create_nav_menu() which creates the menu itself and returns the menu_id wp_update_nav_menu_item() which will create a new menu item if you pass in 0 for the menu item id param.

Then you could use a wp_nav_menu walker class to iterate over each of the existing items in a menu. This website will give you a good reference point to start with that.

http://www.kriesi.at/archives/improve-your-wordpress-navigation-menu-output

So you call the create nav menu function first.

Then implement the nav walker on a given page. Inside your walker class's start_el function, you'll have access to the $item param that will allow you to call wp_update_nav_menu_item() with identical properties as the existing menu, but just pass 0 instead of the item's real id, which will create a new menu item.

The only other property you'll need to consider is the menu-item-parent-id because you'll want that to reference your newly created menu item and not the old one on the other menu. For this you'll need to create an array that keeps track of old id's to new id's.

If you'd like a concrete example of this solution just email me at marcuspope.com and I'll see what I can whip up.

Hope that helps! -Marcus

share|improve this answer
    
I would love a concrete example :) –  cwd Nov 23 '11 at 20:26
    
I'm about 90% complete with a plugin that will do this plus a bunch of other handy menu management features. I'll try to throw a blog post up soon if I can't finish the plugin before the holidays. I'll update this answer when I do :) –  Marcus Pope Nov 28 '11 at 21:56
    
Any luck on getting that plugin working? –  cwd Jan 10 '12 at 17:55
    
Ugh, unfortunately no - I've been sidelined with a series of projects at work and haven't had a chance to complete the code. Here's a code sample that should get you going pastebin.com/hV20hKzJ –  Marcus Pope Apr 12 '12 at 22:14
    
just got back to take a look at this question again and I see you've finished your plugin. Thanks! Only thing I'd love to see is some screenshots on the plugin page :) –  cwd Sep 20 '12 at 18:59
add comment

So I was struggling with a similar issue

  1. I wanted to create lots of menus to displayed based upon permissions... lots of the items were duplicates etc so the easy way was likely to somehow copy something that already existed

  2. I then needed to migrate all those menus to multiple blogs which share the same permissions.

But I am only a half-hearted programmer.

So this is the kludge solution but it seems to work, no programming required

WP has an export function - if you have a clean blog (and maybe even if you haven't) it seems all the menu items get listed in the same part of the export file.

There are only 2 things (possibly) if you are dealing with a single blog that need to be changed, as posts and pages will be skipped anyway due to duplicate slugs

This is the definition for the menu's custom category. You need to change the slug and the CDATA (where is says "FA" in my example)

<wp:term><wp:term_id>3</wp:term_id><wp:term_taxonomy>nav_menu</wp:term_taxonomy><wp:term_slug>FA</wp:term_slug><wp:term_name><![CDATA[FA]]></wp:term_name></wp:term>

Next you need to do a search and replace for all the menu items related to that slug / category.

<category domain="nav_menu" nicename="FA"><![CDATA[FA]]></category>

Again look for the "FA"

Save the XML with a nice clean filename.

Then you use the WP importer plugin to import the XML

YMMV but WFM and saved me a ton of time

share|improve this answer
    
Better than any of the other answers so far, thanks! –  cwd Jan 10 '12 at 17:55
add comment

protected by toscho Jun 18 '12 at 21:11

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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