In This Tutorial:
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.
SSH into your server as the cPanel user that owns the website you would like to work with.
Use the cd command to switch to the document root directory of your website.
Example: cd public_html/wp
NOTE: Be sure to replace public_html/wp with the actual path to the document root directory of your website.Click on the button below for the menu item you would like to add, to expand and display the command used for each one.
Run the following command:
wp menu item add-post primary-nav 39
NOTE: Be sure to replace primary-nav with the actual name of the menu and 39 with the actual post_id of the post you would like to add to your menu.Run the following command:
wp menu item add-post primary-nav 42
NOTE: Be sure to replace primary-nav with the actual name of the menu and 42 with the actual ID of the Page you would like to add to your menu.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.
Run the following command:
wp menu item add-custom primary-nav “My Custom Link” https://www.example.com
NOTE: Be sure to replace primary-nav with the actual name of the menu, “My Custom Link” with the actual title (enclosed in quotes if there are blank spaces in the title) and https://www.example.com with the actual URL you would like to add to your menu.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.
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.
Remove Menu Item
SSH into your server as the cPanel user that owns the website you would like to work with.
Use the cd command to switch to the document root directory of your website.
Example: cd public_html/wp
NOTE: Be sure to replace public_html/wp with the actual path to the document root directory of your website.Run the following command:
wp menu item delete 120
NOTE: Be sure to replace 120 with the actual db_id of the menu item you would like to delete from your menu.[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!