Using WP-CLI to Import and Export Menus from WordPress

Exporting menus can be very tricky in WordPress. Every time I have tried with plugins something never seemed to carry over and I would have to recreate them manually. I recently was working on a client site from Codeable and the menu was so huge it was going to be a seriously monotonous task to recreate it. We decided to make a tool that would make it easier to import and export WordPress menus with WP-CLI by creating a custom package which extends the core WP-CLI menu command.

There was an existing project that we forked to work with newer versions of WordPress and to support custom post types: here is the git repository for the WP-CLI menu import and export package.

Using WP-CLI to Import and Export Menus from WordPress

Installing the WP-CLI menu import and export package is straightforward

wp package install https://github.com/wpbullet/wp-menu-import-export-cli.git --allow-root

Now we can move on to exporting the menus and importing the menus.

Exporting Menus with WP-CLI

Export all menus like this using the –all flag

wp menu export --all --filename=menu.json --allow-root

You can also export by the menu name, slug or term id.

To get this pieces of information you can use this command

wp menu list --allow-root

Output

+---------+------+------+-----------+-------+
| term_id | name | slug | locations | count |
+---------+------+------+-----------+-------+
| 3       | Main | main | primary   | 13    |
| 2       | Top  | top  | secondary | 1     |
+---------+------+------+-----------+-------+

So you can export just the Main menu by name

wp menu export "Main" --filename=main-menu.json --allow-root

You can also export by term_id, so for the Top menu the term is 2 so it would be

wp menu export 2 --filename=top-menu.json --allow-root

If you want to export by slug it is usually the lower case version of the name without any spaces.

wp menu export top --filename=top-menu.json --allow-root

Now let's import the menu exports with WP-CLI.

Import Menus with WP-CLI

Importing the menu export with WP-CLI can be done like this

wp menu import menu.json --allow-root

If your menu already exists and has the same slug or name you will have to delete it before importing the newly exported one.

You can get the name of your menu so you can delete it by using the wp menu list command

wp menu list --allow-root

Output

+---------+------+------+-----------+-------+
| term_id | name | slug | locations | count |
+---------+------+------+-----------+-------+
| 2       | Main | main | primary   | 163   |
+---------+------+------+-----------+-------+

You can delete the menu in 3 ways, by specifying the term_id, the name or the slug.

This is how you do it by name

wp menu delete "Main" --allow-root

Delete the WordPress menu by term_id

wp menu delete 2 --allow-root

And finally delete the menu by slug

wp menu delete main --allow-root

Now you can try your import again

wp menu import menu.json --allow-root

Sources

wp menu list
wp menu delete