How to Add and Delete Menu Items Using WP-CLI

In This Tutorial:

Add Menu Item Delete Menu Item

WordPress Hosting plans offered by InMotion Hosting feature WP-CLI. This means that you can manipulate various aspects of your WordPress website, directly from the command line. In this guide, you will learn how to use the wp menu command to add and delete menu items, using WP-CLI.

Create Menu Item

If you would like to add an item to your menu, you can use the wp menu item sub-command. The following steps will guide you through the process of adding a custom URL, a Post, and a Page to your menu, using WP-CLI.

  1. SSH into your server as the cPanel user that owns the website you would like to work with.

  2. Use the cd command to switch to the document root directory of your website.

    Example: cd public_html/wp

  3. Click on the button below for the menu item you would like to add, to expand and display the command used for each one.

    1. Run the following command:

      wp menu item add-post primary-nav 39

    2. The output will be similar to the example below:

      exampl3@example.com [~/public_html/wp]# wp menu item add-post primary-nav 39 Success: Menu item added.

    1. Run the following command:

      wp menu item add-post primary-nav 42

      The output will be similar to the example below:

      exampl3@example.com [~/public_html/wp]# wp menu item add-post primary-nav 42 Success: Menu item added.

    1. Run the following command:

      wp menu item add-custom primary-nav “My Custom Link” https://www.example.com

      The output will be similar to the example below:

      exampl3@example.com [~/public_html/wp]# wp menu item add-custom primary-nav "My Custom Link" https://www.example.com Success: Menu item added.

Remove Menu Item

  1. SSH into your server as the cPanel user that owns the website you would like to work with.

  2. Use the cd command to switch to the document root directory of your website.

    Example: cd public_html/wp

  3. Run the following command:

    wp menu item delete 120

    exampl3@example.com [~/public_html/wp]# wp menu item delete 120 Success: Menu item(s) deleted.

Was this article helpful? Let us know!