My first office job was doing manual data entry in the water department of the City of Olympia, which is fine enough for a 22 year old without many skills. However, for a senior developer like myself, repetitive tasks like creating or modifying menus just makes me feel dumb, consumes time, and is still prone to manual errors. Tools like WP-CLI (WordPress Command Line Interface) and automation scripts can drastically reduce effort and improve accuracy. With the help of large language models like ChatGPT, even non-developers can quickly automate mundane tasks such as building out a large menu.

In this tutorial, I’ll walk you through how I automated the task of creating a WordPress navigation menu from a Shopify site using a combination of Bash, WP-CLI, and ChatGPT.

Step 1: Installing WP-CLI

Most of the managed hosting services already have WP-CLI installed. As does Local (for local dev). If you haven’t already installed WP-CLI, here’s a quick setup:


1. Download and install WP-CLI:

Bash
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp


2. Verify installation:

Bash
wp --info

This should confirm that WP-CLI is ready to be used for automating WordPress tasks.


Step 2: Let’s add menu items via WP CLI

I have a client who has their main website at Shopify, but keeps their legacy blog on WordPress. The goal was to reproduce the interface between the platforms as much as possible, so that a user to their high ranking blog posts has the ability to navigate through the detailed product menu and go immediately to the product category they are interested in.

So, the task at hand was to duplicate a navigation menu with over 117 items into our new WordPress site. Manually adding each item through the WordPress dashboard would have taken too long, and is boring, so I decided to automate this with WP-CLI and a Bash script.

I recommend taking a look at the official documentation for this, so you know what is possible. Here’s the basic WP-CLI command that adds a custom menu item to WordPress:

Bash
wp menu item add-custom <menu-slug> 'Link Text' 'http://example.com'


That’s fine if I only have a few to do, but for a large number of menu items, especially with submenus, writing a bash script to handle the menu hierarchy is more efficient.

Step 3: Creating a Bash Script for Menu Automation

Instead of manually adding each item, I created a custom Bash script to automate the process. Here’s a breakdown of the script logic:

  1. Define Menu Name and Base URL – Set variables for the name of the WordPress menu and the base URL for the links. Make sure that the menu exists first that you are referencing in the menu_name variable, or else you’ll get error messages of Error: Invalid menu.
  2. Parent and Child Menu Items – Use arrays to store parent menu items and their corresponding URLs. For the child items, indexed arrays (not associative) ensure the correct order is maintained. Here’s why: in Bash, indexed arrays guarantee order, whereas associative arrays do not, which I learned the hard way the first time around when using an associative array structure.
  3. Add Menu Items Function – A function add_menu_items was created t handle the logic for adding parent and child menu items. This function uses the WP-CLI command inside a loop to ensure the correct items are added in order.

Below is the general structure of this bash script. You can download a copy of it here.

Bash
#!/bin/bash

# Variables
menu_name="shopify-mirror"
base_url="https://shopify-site.com"

# Function to add menu items
add_menu_items() {
    parent_name="$1"
    parent_url="$2"
    child_items=("${!3}")

    # Add parent menu item
    parent_id=$(wp menu item add-custom $menu_name "$parent_name" "$parent_url" --porcelain)

    # Add child menu items
    for child in "${child_items[@]}"; do
        wp menu item add-custom $menu_name "$child" "$base_url/$child" --parent-id=$parent_id
    done
}

# Parent and child arrays
parent_1_children=("Child1" "Child2" "Child3")
parent_2_children=("ChildA" "ChildB")

# Add parent and child items
add_menu_items "Parent 1" "$base_url/parent-1" parent_1_children[@]
add_menu_items "Parent 2" "$base_url/parent-2" parent_2_children[@]

# Add standalone parent item
wp menu item add-custom $menu_name "Parent 3" "$base_url/parent-3"

Step 4. Running the Script

Once the script was finalized, I saved it to a local test WP instance and ran it from the root. It’s important to test this out first with a small sample size, so it’s easy to delete the entries afterwards. Don’t forget to backup your database first.

Instructions:

Make it executable

Bash
chmod +x add-full-menu.sh

Run the script

Bash
./add-full-menu.sh

This script connected to the WordPress database using WP-CLI and correctly added my test menu to WordPress, replicating both parent and child menu items automatically. Now I’m ready to do the real thing.

Step 5. Extract and reformat the original menu

Using the inspector tool, I grabbed the portion of code that contained the target menu.

After that, I saved the file into an html document called og-menu.html. I then went into claude.ai (which I prefer for writing and coding tasks) and gave it the following prompt

"Can you take the attached html file, which is of a website navigation menu, and strip out all the unnecessary classes and other markup. All I want is a list of menu titles, their links, and menu children as sub lists. Please format this as a markdown file. I'll use this later to generate a new menu on another CMS."

This created a cleaned up file I would be able to leverage using more AI. Here’s a sample of it in html.

Step 6: Using ChatGPT for Automation Assistance

Instead of manually rewriting the Bash script, I leveraged ChatGPT to assist me in automating the script generation. I uploaded both files to the UI and gave it the prompt:

I would like you to help me re-write an existing bash script which I'll use with WP CLI to create a lot of menu items. Please take the attached file "sample-html-menu-export.html" and format all of the menu items and sub menu items according to the logic contained within "sample-add-menu-script.sh". 

Here’s how ChatGPT helped:

1. Parsing markdown : Since I had converted the HTML of the Shopify navigation into markdown, which was originally structured in <ul> and <li> tags, this was easy to convert into an associative array structure.

2. Script Creation: After providing the relationships, ChatGPT assisted me by generating the bash script that used WP-CLI to add these menu items. ChatGPT parsed the markdown file into structured data. This allowed me to quickly generate the parent-child relationships needed for the bash arrays.

Step 7: Run our customized bash script

Run the script

Bash
./add-full-menu.sh

I was real happy to see the loop working:

I checked the WP backend and was real pleased with the results. Even if this initial effort took just as much time to do as the manual method, we’ve now got a documented and repeatable process for saving time in the future

In Conclusion

WP-CLI enables developers to automate WordPress tasks like managing posts, users, and menus directly from the command line, while ChatGPT can expedite the process of creating complex scripts and workflows. When combined:

  • ChatGPT can assist with code generation, script logic, and troubleshooting by interpreting your input and rapidly providing usable code.
  • WP-CLI handles the execution of commands in a precise manner.

This combination is especially useful when dealing with large datasets, complex hierarchical structures, or repetitive tasks like replicating a menu across platforms.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.