How to Add and Delete Menu Items Using WP-CLI

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:

      [email protected] [~/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:

      [email protected] [~/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:

      [email protected] [~/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

    [email protected] [~/public_html/wp]# wp menu item delete 120 Success: Menu item(s) deleted.

Learn more about WP-CLI, the command-line interface for WordPress, and how to streamline your WordPress maintenance workflows!

JB
John-Paul Briones Content Writer II

John-Paul is an Electronics Engineer that spent most of his career in IT. He has been a Technical Writer for InMotion since 2013.

More Articles by John-Paul

Was this article helpful? Join the conversation!